I have created a macro that display a text and after that it flush stdout. The problems is how to force to clear old printed text if the text is more longer than the newer.
Example : If I try to print a string with 50 characters after that I need to overwrite all the text and rewrite text with 25 characters. All the time I have part of first text is always printed because the second is more shorter.
Also I need to insert \r
at the end of each line, how to add in my macro?
#include <stdio.h>
#include <string.h>
// I need to add "\r" to macro instead of to add it for all string
#define MESSAGE( fmt, args...) \
do { setbuf(stdout, NULL); fprintf(stdout, fmt, ## args); fflush(stdout); sleep(5); } while (0)
int main()
{
MESSAGE( "the application is started successfully\r");
MESSAGE( "the application will be stopped soon\r");
MESSAGE( "app stopped: ok\n\r");
return 0;
}
Result:
./test2
app stopped: ok will be stopped soonlly
The expected result:
./test2
app stopped: ok