I would like to create a small function, which should show the current status in percentage in the console. The function should only show the current value in the line.
So I have to remove the last output. Is that possible? The best case would be some ANSI C stuff, the program should work on linux and windows.
I have found some escape sequences like "\033[J", but it dose not work.
Edit
Well I tried it with:
void PrintProcessBar(int i, int n) {
float ratio = i / (float)n;
printf("%.3d\b\b", (int)(ratio * 100));
fflush(stdout);
}
But it just print lots of zeros… What went wrong? If I use three \b I get nothing.
Solution
float ratio = (i / (float)n) * 100;
printf("\b\b\b\b");
fflush(stdout);
printf("%.3d%c", (int)ratio, 37);
fflush(stdout);
i = current position.
n = max runs.
I use 4 \b because, at the first call of the function it will remove 4 spaces.
Greetz.
\bis the escape sequence for backspace. you can use it with spaces to delete a line.For example:
This should print 98%, and then replace it with 99%.