Rayne
Rayne

Reputation: 14977

Show progress indicator

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.

Upvotes: 1

Views: 220

Answers (2)

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215261

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

Upvotes: 3

Vinicius Kamakura
Vinicius Kamakura

Reputation: 7778

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.

Upvotes: 2

Related Questions