问题
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