Progress bar principle

Source: Internet
Author: User


First, write a simple progress bar under Linux

Progress bar Implementation principle:

1), to achieve dynamic growth

Defines an array and sets a counter that, when the number increases, adds an element to the character array each time and overwrites the previous array when printed.

2), to ensure that print information output in one line

Each time the cursor is positioned to the leftmost, print from the cursor, use ' \ n ', enter

3), the effect of replication dynamic implementation

Define an array

Char *index= "-/|\\";

Output time index[count%4];

4), to achieve the same print length%-100s

5), the effect of copying to people

Every time the program is output, the program hangs for a while

Second, the implementation code:

#include <stdio.h>

#include <string.h>

Void proc ()

{

Int i=0;

Char bar[102];

Const Char *lable= "-\\|/";

Bar[0]= ' ";

while (i<=100);

{

Printf ("[%-101s][%d%%][%c]\r", bar,bar,lable[i%4]);

Fflush (stdout);

bar[i++]= ' # ';

Bar[i]= ' ";

Usleep (10000);

}

Printf ("\ n");

}

Int Main ()

{

Proc ();

return 0;

}

Third, the result chart:

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.