Implement a simple progress bar "go" in Linux

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/yuehailin/article/details/53999288

Talking about the progress bar, in fact, we often see, for example, when you download video or file, you are prompted to download the current progress is what we are going to say today, progress bar simulation is quite simple, but to do more practical or need to pay attention to a lot of places, down I will step-after-depth analysis of progress bar to achieve.

1. At first, this code was written, explaining the following points:

1> First we need to fix [] the left and right sides, pre-leave space in the middle, and then fill with "=".           Here printf ("[%-100s]\r", str); Formatted output,'-' for left alignment,100 for fixed column width, and then ' \ R ' for carriage return, that is, each time you print the cursor back to the beginning of the definition ,' \ n ' and ' \ R ' are two concepts:

' \ n ' indicates a newline, which is the next line of the current line, where the cursor points to the beginning of the next line; ' \ R ' means the carriage return, that is, the cursor is back to its first position.

2> If not sleep time, the results are all printed out, but we want to make it a little slower, after all, is the progress bar, and the Linux system default sleep time unit is seconds (s), so that the interval is too long, so there is usleep, it is a subtle, The header file is in # include <unistd.h>, this information does not understand the man a bit;

3> The following code when we observe the phenomenon will find is 0.1s in the display, but it is a section of the display, which makes people very depressed, and finally found to be the reason for printf, printf is to write the output of the content into the buffer, and then refresh.

Let's start by introducing some of the buffering mechanisms in Unix about standard IO:

<3.1> full buffering, full buffering refers to the fact that the system does not perform the actual IO operation until the standard IO buffers are filled; Note that for files residing on disk, the standard IO libraries are usually fully buffered.

<3.2> buffer, in which case the standard IO performs an IO operation when a newline character is encountered in the input and output, and note that when the stream involves a terminal, the row buffer is usually used.

<3.3> unbuffered, unbuffered refers to the standard IO library does not buffer the characters to store; Note that standard error stream stderr is usually unbuffered.

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) The buffer fills up;
(2) The characters written are ' \ n ', ' \ R ';
(3) Call Fflush to manually flush the buffer;
(4) when calling scanf to read data from the buffer, the data in the buffer is also flushed;
Satisfies one of the above 4 conditions the buffer will refresh
Here is the code implementation:

Take a look:

2. Know the reasons for a paragraph shown above, we will improve it, by fflush This function can solve the problem.

The fflush is used to empty the buffer stream, so that all the contents of the buffer are immediately output, and can be refreshed instantly, which just satisfies the real-time of the progress bar.


Here is the effect show:


3. The above basically achieved the effect we want, but still not good, when you download the file when someone else has a percentage to indicate how much you downloaded, and we have only a progress bar, how much to go forward to estimate, so much bad ah, so we also come to realize this way.



4. The above implementation seems to have the effect, but we will still feel some lack of, for example, sometimes the progress bar will be stopped, this time we can hardly know whether the program is continuing to run or stuck, below I through a dynamic rotation of the icon to optimize it.

Rotate icon yourself can be set, "-\ \ \ | /"or" | /-\ \ "Can



5. Finally, I used two commands when I ran the code, make and./proc, that's because I wrote a makefile file in advance, which makes it easier for us to run the code.

Here's a look at the makefile file:


As a good habit, remember every time you finish executing./proc

Implement a simple progress bar "go" in Linux

Related Article

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.