开发者

Show progress indicator

开发者 https://www.devze.com 2023-03-18 10:50 出处:网络
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 \"Searc

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

精彩评论

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