开发者

Show progress indicator

I'm using C in Linux. How do I s开发者_JAVA百科how a progress indicator that will let me know when the program (or parts of the program) will complete? For example, it could be something like "Searching...67%" and the percentage will keep increasing until the Searching portion ends.

Thank you.


Write a '\r' character to stdout to return the cursor to the beginning of the line so you can overwrite the line. For example:

for (i=0; i<100; i++) {
    printf("\rSearching...%d%%", i);
    fflush(stdout);
    sleep(1);
}


I believe if you do something like:

while (perc < 100) {
    printf("Searching... %d%%\r", perc); 
    fflush(stdout);
    //do work
}

the fflush() is necessary to avoid the line buffering. Note that I am using \r and not \n.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜