Use the make command to compile the progress bar program under Linux.

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

#include<stdio.h>
#include<unistd.h>
#include<string.h>

void progress()
{
    int i = 0;
    char bar[102];
    memset(bar,0,102*sizeof(char));
    const char* lable="|/-\\";
    while(i <= 100)
    {
        bar[i] = '#';    
        printf("[%-101s] [%d%%] [%c]",bar,i,lable[i%4]);
        fflush(stdout);
        usleep(100000);
        i++;
    }
    printf("\n");
}

int main()
{
    progress();
    return 0;
}

As shown in the figure:
Write picture description here

The small details that need to be paid attention to in this code:
1. const char* lable=”|/-\\”; directly inputting a \ will be considered as an escape by the system, so input \\
2. printf("[%-101s ] [%d%%] [%c]”,bar,i,lable[i%4]); The %% here is the same as above, to prevent escaping. i% 4 prevent overflow
3. fflush (stdout); parameter is the standard output stream
4. Since the default sleep seconds, the test is not easy, usleep default microseconds
Finally, debugging, mymakefile create a file, touch mymakefile the File to edit vim mymakefile.

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

As shown in the figure:

Then execute the make command to compile the progress_bar.c file, make -f mymakefile, which generates the myprogress_bar file, and execute it with ./myprogress_bar. If you want to recompile, you need the make -f mymakefile clean command to clean the file progress_bar first, and then use make to compile.
As shown:
Write picture description here

Guess you like

Origin blog.csdn.net/lxp_mujinhuakai/article/details/69396533