Linux Simulation Implementation progress bar

Source: Internet
Author: User


What is a progress bar? The progress bar is when you download files, software, games and other progress, it will show you how much you can download this thing.

Because of my curiosity, I realized this thing under Linux.

My idea of realization:

1. To an array, this array must be able to hold the next 100 characters.

2. Then read the array as a line.

3. The final standard output is on the screen.

Attention:

1. We want to print a progress bar is definitely 100%, so it is necessary to be able to just under 100 characters of the array, but need to be aware of is to open up a word descriptors '/0 ', open up a good later also need to initialize all to '/0 ' to prevent garbled characters when the output string.

2. The most important thing is to add printf ("/R"), this is used to only enter the non-newline, can achieve the function is to buffer the value of the inside is always only one row, does not appear each time to output a string of characters, so that the core function of the progress bar can be achieved.

3. When printing the progress bar, you need to pin both sides of the box, so write%100s but need to add a minus sign, because it is printed from the right to start

Implementation code:

#include <stdio.h> #include <string.h>void proc () {int rate = 0;const char *lable = "|/-\\"; char bar[102]; memset (bar, ' + ', sizeof (bar));//Initialize while (rate <=) {Bar[rate] = ' = ';p rintf ("[ -101s][%d%%][%c]\r", bar, rate, Lable[rate% 4]); Fflush (stdout);//Standard output rate++;usleep (10000);} printf ("\ n");} int main () {proc (); return 0;}

Output Result:

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M02/82/63/wKioL1dT5HehpO6GAAAMmKHYGsE340.png-wh_500x0-wm_3 -wmp_4-s_2828432953.png "title=" Qq20160605163224.png "alt=" Wkiol1dt5hehpo6gaaammkhygse340.png-wh_50 "/>

Linux Simulation Implementation 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.