C remove printf output

I would like to create a small function that should show the current state as a percentage in the console. The function should show only the current value in the string. Therefore, I need to remove the last output. Is it possible? The best case is some ANSI C stuff, the program should work with linux and windows.

I found some escape sequences, such as "\033[J", but the dose does not work.

Edit

Well, I tried:

void PrintProcessBar(int i, int n) {
    float ratio = i / (float)n;    
    printf("%.3d\b\b", (int)(ratio * 100));
    fflush(stdout);
}

But he just prints a lot of zeros ... What went wrong? If I use three \b, I get nothing.

Decision

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 works.

I use 4 \bbecause the first time the function is called, it will remove 4 spaces.

Greetz.

+5
source share
2

\b - escape- backspace. .

:

printf("98%");
fflush(stdout);
// sleep some time
printf("\b\b\b99%");
fflush(stdout);

98%, 99%.

+10

, , \r, . . \n, .

\r , , , (, ), .

+6

All Articles