Q:

How to make a "progress bar" using printf?

Many command line tools implement text-based progress bar. Like rpm installing:

installing ##############[45%]

the # grows with the percentage, while keeps itself in a single line. What I want is something similar: I need a progress indicator taking just one line, that is to say, when percentage grows, it got overwritten, instead of make a new line(\n).
I tried this:
   #include <stdio.h>

   int main (){
       int i = 0;
       for (i = 0; i < 10000; i++){
           printf("\rIn progress %d", i/100);
       }
       printf("\n");
   }

\r works to overwrite the single line. However, \r brings cursor to the beginning of line and printf brings cursor to the end, which result in a rapidly waving cursor. You guys can feel it by a little compiling. Can Anyone come up with alternatives to avoid this issue? 

A:

This is a problem of the stdout stream being buffered. You have to flush it explicitly (implicit flushing occurs with a \n) using fflush(stdout) after the printf():
fflush(stdout);

A:

Here is how the rpm did it, maybe you can write a similar for your own purpose: printHash.
rpm use \b instead of \r to erase the output line character by character. 

