The progress bar in the project realizes the imaginary

Source: Internet
Author: User

On the implementation of the progress bar, the main point is dynamic modification, and is in-line buffer update.

In this respect, you need to understand the row buffer problem with printf ().

Can do the following verification

#include <stdio.h><stdlib.h>int  main () {    //  printf ("test") ;        Case1:3 seconds after the output    printf ("test\n");        // case2: Output now    Sleep (3);     return 0 ;}

The difference between the two case is \ n, so the crux of the problem is \ n.

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 fills up.
2 The characters written are ' \ n ', ' \ R '.
3 Call Fflush to flush the buffer manually.
4 Call scanf the data in the buffer is also flushed when the data is being read from the buffer.

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.

For the above points, it is easy to verify, here is not separate talk about
About the progress bar.

With the understanding of the buffer problem, we can grasp the timing and way of the main update buffer.

When the progress bar advances, the update buffer is re-written, and the method is \ r. Because our progress bar is a line to

Code implementation

#include <stdio.h>#include<string.h>voidproc () {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++; Sleep (1); }}intMain () {proc (); return 0;}

The problem with this progress bar is just a simple row buffer understanding and a short project experience. The progress bar in the actual project is not so.

Personal understanding, to think:: Because the actual program in the progress bar is required to meet the tool, the specific function of the progress achieved. So the implementation of the progress bar is not a valid action on a timeline.

For example, file compression, for a few points in the process of the relative control, and then a progress bar as a companion process, the use of inter-process relations (or observer mode), the program is a step forward, report to the progress bar program to advance a step.

To achieve real progress to grasp.

Again such as network file download, first: You can determine the exact size of the read file, at this time, is based on the network transfer rate, file transfer ratio to update follow-up progress bar program.

The progress bar in the project realizes the imaginary

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.