A series of problems caused by implementing the Linux progress bar

Source: Internet
Author: User

Implementation progress bar under Linux

The code is as follows:

 1  #include <stdio.h>  2  #include <unistd.h>  3 void  Progess ()   4 {  5         char arr[ 101];  6         memset (arr, '% ', sizeof (arr)/sizeof ( char));   7         char state[5]= "|/-|\0";   8         int i=0;  9          for (i=0;i<100;++i)  10          { 11                  arr[i]= ' * '; 12                  printf ("[%-100s][%d%%][%c]\r", arr,1+i,state[i%4]); 13  14       &nbSp;          usleep (100000);//us 15                  fflush (stdout);  16          } 17          printf ("\ n");  18  19 } 20 int main ()  21 { 22          progess (); 23          return 0; 24 }

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M02/82/5E/wKioL1dSzk_hYyexAAAb_KwjEjQ560.png-wh_500x0-wm_3 -wmp_4-s_2867756899.png "title=" progress bar. png "alt=" wkiol1dszk_hyyexaaab_kwjejq560.png-wh_50 "/>

Implementing the progress bar requires familiarity with the following knowledge points

1, \ r

With \ r as the output control character

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M02/82/60/wKiom1dS2VmhEtMNAAAjVgwzKiY241.png-wh_500x0-wm_3 -wmp_4-s_3135487655.png "title=" direct output r.png "alt=" Wkiom1ds2vmhetmnaaajvgwzkiy241.png-wh_50 "/>

Operation Result:
650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M01/82/5F/wKioL1dS2omBDVRTAAAP6w21rZY010.png-wh_500x0-wm_3 -wmp_4-s_2082920232.png "title=" Output r.png "alt=" Wkiol1ds2ombdvrtaaap6w21rzy010.png-wh_50 "/>

Obviously, there are no output results.

Add an output after the first line of output

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M00/82/60/wKiom1dS20OzoL8PAAAoB_tV4hM726.png-wh_500x0-wm_3 -wmp_4-s_799332296.png "title=" one r a direct output. png "alt=" wkiom1ds20ozol8paaaob_tv4hm726.png-wh_50 "/>

Output Result:

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M01/82/5F/wKioL1dS3YuxJO77AAARyrGiNw4275.png-wh_500x0-wm_3 -wmp_4-s_3949198230.png "Title=" a r a direct output results display. png "alt=" wkiol1ds3yuxjo77aaaryrginw4275.png-wh_50 "/>

Add \ n to the second line of output

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M01/82/60/wKiom1dS3W2wJNbwAAAkxYk9EwE408.png-wh_500x0-wm_3 -wmp_4-s_1345973076.png "title=" one R one N output. png "alt=" wkiom1ds3w2wjnbwaaakxyk9ewe408.png-wh_50 "/>

Output Result:

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M01/82/5F/wKioL1dS3pHC8IbYAAASht9xXAM400.png-wh_500x0-wm_3 -wmp_4-s_3115517677.png "title=" one R one n output results display. png "alt=" wkiol1ds3phc8ibyaaasht9xxam400.png-wh_50 "/>

You can see that the second row will overwrite the first line of output

This gives the first output a \ n

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M02/82/5F/wKioL1dS3zXjUVHDAAAkYu_joKM921.png-wh_500x0-wm_3 -wmp_4-s_1453056496.png "Title=" two n a r.png "alt=" wkiol1ds3zxjuvhdaaakyu_jokm921.png-wh_50 "/>"

Output Result:

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M01/82/5F/wKioL1dS30mCs2bmAAARYPRjHEk418.png-wh_500x0-wm_3 -wmp_4-s_2934113604.png "Title=" two n one r results display. png "alt=" wkiol1ds30mcs2bmaaaryprjhek418.png-wh_50 "/>

As can be seen from the above procedures,

Carriage return newline (\ n): Each time the cursor moves to the beginning of the next line at the position;

Line break (\ r): Each time the cursor moves to the beginning of the bank

In the input \ r, the cursor moves to the beginning of the line, the input \ n cursor wraps and moves to the beginning of the next line, equivalent to \n\r. So we added a sentence fflush (stdout) in the program to ensure that the program can output the effect, without adding fflush (stdout), the program can still output the results, but the results show that the buffer is full when the refresh.

2, line buffer, fflush

printf is a row buffer function that is written to the buffer before it satisfies the condition before the buffer is brushed into the corresponding file, and the condition of the brush buffer is as follows:

1 buffer Filled

2 characters written in ' \ n ' \ r '

3 calling Fflush to flush buffers manually

4 Call scanf when you want to read data from a buffer, the data in the buffer is also flushed

One of the 4 conditions above satisfies that the buffer will refresh, that is, printf will actually call write to write

When we execute the printf process or the thread ends, it will call flush to flush the buffer, so the program ends and refreshes.

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M00/82/60/wKiom1dS1ILAfEGVAAAl6FWu1xQ025.png-wh_500x0-wm_3 -wmp_4-s_1799218864.png "title=" output test. png "alt=" wkiom1ds1ilafegvaaal6fwu1xq025.png-wh_50 "/>

Output results

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M00/82/5F/wKioL1dS1duyRUp6AAAq-HJtdew392.png-wh_500x0-wm_3 -wmp_4-s_4294846715.png "title=" output result. png "alt=" wkiol1ds1duyrup6aaaq-hjtdew392.png-wh_50 "/>

When the program is running because of the delay, the output is not one character output, but one output of 1024 characters at a time, thus proving that the row buffer size is 1024 bytes.

3, Sleep,usleep

That is, the sleep () function is in seconds, sleep (1), or dormant for 1 seconds, under Linux (the library used by GCC). While the sleep () function under MFC is in milliseconds, sleep (1000) is dormant for 1 seconds. If you sleep with a subtle unit under Linux, you can use the thread sleep function: void Usleep (unsigned long usec).



This article is from the "Qin-wang" blog, make sure to keep this source http://10810196.blog.51cto.com/10800196/1786171

A series of problems caused by implementing the 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.