Show progress indicator

邮差的信 提交于 2019-12-11 12:20:33

问题


I'm using C in Linux. How do I show 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.


回答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.




回答2:


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);
}


来源:https://stackoverflow.com/questions/6591264/show-progress-indicator

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!