Small program in Linux--progress bar

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.