简体   繁体   English

请提供任何帮助,我正在尝试将此 C++ 代码转换为 C 代码,但我遇到了 fflush() function 代码的问题

[英]Any help please, I'm trying to convert this C++ code to C code, but I'm having problem with fflush() function

Any help please, I'm trying to convert this C++ code to C code, but I'm having problem with fflush() function, or if there is another code functions like this, please do share it.请提供任何帮助,我正在尝试将此 C++ 代码转换为 C 代码,但我遇到了 fflush() function 代码的问题,或者如果有另一个代码,请分享它。

    #include <iostream.h>
    #include <time.h>
    #include<dos.h>
    int main()
    {
        cout << "Loading";
        cout.flush();
        for (int j=0; j<2; ++j) {
            for (int i = 0; i < 3; i++) {
                cout << ".";
                cout.flush();
                sleep(1);
            }
            cout << "\b\b\b   \b\b\b";
        }
    
        return 0;
    }
//________________________________________________________________________

    #include <stdio.h>>
    #include <unistd.h>
    int main()
    {
       printf("Loading");
        fflush();
        for (int j=0; j<2; ++j){
            for (int i = 0; i < 3; i++) {
                printf(".");
                fflush();
                sleep(1);
            }
           printf("\b\b\b   \b\b\b");
        }
        return 0;
    }

fflush takes an argument - the stream it is supposed to flush. fflush接受一个参数 - stream 它应该刷新。 You'd need to use fflush(stdout) .您需要使用fflush(stdout)

Another problem is that the C++ code is semantically wrong too.另一个问题是 C++ 代码在语义上也是错误的。 Rather than using cout or printf to stdout , for such diagnostic messages one would be using cerr / stderr , which should be unbuffered anyway.而不是使用coutprintfstdout ,对于这样的诊断消息,人们将使用cerr / stderr ,无论如何都应该是无缓冲的。 using that you wouldn't probably need the fflush .使用它你可能不需要fflush Ie IE

#include <stdio.h>
#include <unistd.h>


int main(void)
{
    fprintf(stderr, "Loading");
    for (int j=0; j<2; ++j){
        for (int i = 0; i < 3; i++) {
            fprintf(stderr, ".");
            sleep(1);
        }
        fprintf(stderr, "\b\b\b   \b\b\b");
    }
}

fflush needs file pointer as argument. fflush需要文件指针作为参数。 This would flush the local buffer data(stored in primary memory - mostly dram) corresponding to the file into the actual file stored in the secondary storage.这会将与文件对应的本地缓冲区数据(存储在主 memory - 主要是 dram 中)刷新到存储在辅助存储中的实际文件中。 For your case use fflush(stdout)对于您的情况,请使用fflush(stdout)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM