Write a simple progress bar under Linux and compile with the make command.

Source: Internet
Author: User
Tags usleep

First create a new file, Touch progress_bar.c execute the vim progress_bar.c command, write the progress bar of the program. Write into a progress bar program:

#include <stdio.h>#include <unistd.h>#include <string.h>voidProgress () {inti =0;Charbar[102];memset(Bar,0,102*sizeof(Char));Const Char* lable="|/-\\"; while(I <= -) {Bar[i] =' # ';printf("[%-101s] [%d%%] [%c]", bar,i,lable[i%4]);        Fflush (stdout); Usleep100000);    i++; }printf("\ n");}intMain () {progress ();return 0;}


Small details to note in this code:
1. const char* lable= "|/-\\"; The direct input of a \ will be considered escaped by the system, so enter \ \
2. printf ("[%-101s] [%d%%] [%c]", bar,i,lable[i%4]), here the percent of the same, to prevent escaping. rate%4 Prevent overflow
3. fflush (stdout); parameter is standard output stream
4. because sleep default unit is seconds, not easy to test, usleep default unit is microseconds
Finally, debug, create a mymakefile file, touch mymakefile to edit the file vim Mymakefile.

myprogress_bar:progress_bar.c    g++ -o myprogress_bar progress_bar.c:PHONY clean    clean:    rm -f myprogress_bar

:

then execute the make command, compile the progress_bar.c file, Make-f mymakefile, generate the Myprogress_bar file and execute it with./myprogress_bar. If you want to recompile, you will need to make-f the mymakefile Clean directive to clear the file Progress_bar before compiling with make.

Write a simple progress bar under Linux and compile with the make command.

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.