简体   繁体   English

在 c 程序中打印有延迟?

[英]Printing in c program with delay?

I'm trying to print something like:我正在尝试打印类似的东西:
Exiting...退出...
into a terminal when the program ends, but the way I want it to be outputted to the terminal is each dot after "Exiting" needs to be printed with a slight delay after each other in the same line.程序结束时进入终端,但我希望它输出到终端的方式是“退出”之后的每个点都需要在同一行中彼此稍有延迟地打印出来。 Like it is loading to put it that way.就像那样说是在加载。 But I have no idea how to make it print each of them separately without the whole line to be printed all at the same time.但是我不知道如何让它单独打印它们而不同时打印整行。 I'm mot sure if that is possible in c in the first place.首先,我不确定在 c 中是否可行。 I've tried adding delay between each printf using time but that did not work.我试过使用时间在每个 printf 之间添加延迟,但这没有用。

You can use it like this -你可以像这样使用它 -

cout<<"Exiting";

cout.flush();
for (int j=0;j<1;j++) {
    for (int i = 0; i < 3; i++) {
        cout << ".";
        cout.flush();
        sleep(1);
    }
    cout << "\b\b\b   \b\b\b";
}
cout<<"\n\n";
sleep(1);

I was stuck with the same problem where I had to print something with delay.我遇到了同样的问题,我不得不延迟打印一些东西。 This thing works flawlessly.这东西完美无缺。

PS - Use the required Header Files. PS - 使用所需的 Header 文件。

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

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