Before talking about the matter, first science in what is a carriage return what is a newline?
What we usually call a carriage return is jumping from the end of a line to the beginning of another line, but in fact it is done by a carriage return and a newline of two actions, that is, the work done on the keyboard <enter>. But in fact, the carriage return is the cursor back to the beginning, line wrapping is just the cursor line down. (Carriage return \r,r is return; newline \n,n is newline)
For a program running in a shell, printf () is row-buffered by default. This means that printf outputs the content to the buffer before it is delivered to the screen. When the character is written with a line break \ n, the buffer immediately outputs what is stored in it. If you do not encounter \ n, the program will output the contents of the buffer after the run is finished. If you want the contents of the buffer to be output without encountering \ n, you can use Fflush (stout) to force the flush.
The following is a simple implementation of the Linux progress bar:
#include <stdio.h>#include<string.h>intPro () {intRate =0; Charbar[102]; memset (Bar,' /',sizeof(bar)); Const Char* Flag ="|/-\\"; while(Rate <= -) {Bar[rate]='='; printf ("[%-101s][%d][%c]\r", bar,rate,flag[rate%4]); Fflush (stdout); Rate++; Usleep (100000); } printf ("\ n");} intMain () {Pro (); return 0;}
^_^ implementation results such as:
The program is not very complex, as long as you can understand the buffer mechanism and line-break return is very good operation ~
Small program in Linux--progress bar