This is my sample program for simulating a Loading progress. The only problem I'm facing right now is clearing my previous output of "Loading: %i"
/* loading program */
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#define TIMELIMIT 5
int main()
{
for(int i = 0, j; i < 5; ++i) {
j = i;
printf("Loading: %i%%", ++j);
sleep(1);
//system("bash -c clear"); not working
//printf("\033[2J\033[1;1H"); clears whole screen with no output at all
}
return(0);
}
if you print \\r
instead of \\n
using printf
, then it will return to the beginning of the same line instead of the next line. Then you can re-print the new status on top of the old line.
Anyway here is the correct code. Thanks to Mobius and Dmitri.
/* loading program */
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#define TIMELIMIT 5
int main()
{
for(int i = 0, j; i < TIMELIMIT; ++i) {
j = i;
printf("Loading: %i%%", ++j);
printf("\r");
fflush(stdout);
sleep(1);
}
return(0);
}
I think the answer to this question is system("reset"). This is the command you're looking for
reset
this command clears your whole terminal.
and your code go like this,
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#define TIMELIMIT 5
int main()
{
int i,j;
for(int i = 0, j; i < 5; ++i) {
j = i;
system("reset");
printf("Loading: %i%%\n", ++j);
sleep(1);
//system("bash -c clear"); not working
//printf("\033[2J\033[1;1H"); clears whole screen with no output at all
}
return(0);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.