I have been trying to use fflush to make a progress bar.
To test fflush, I wrote the small code below.
It works as it supposed to when I uncomment “sleep(1);”
but it works in an unexpected way if
it remains commented out. It prints the first dash, waits than prints all remaining
9 of them and quits.
I don’t understand why it makes a difference.
int main()
{
int f,j;
j =0;
for(f=0;f<10;f++)
{
printf("-");
fflush(stdout);
while(j++<1000000000);
//sleep(1);
}
}
Change your for loop like so:
from:
to: