The first applet in Linux - progress bar

line buffer concept

First look at some codes:

    #include <stdio.h>
    
    intmain()
    {
        printf("hello Makefile!\n");
        sleep(3);
        return 0;
    }
   

Phenomenon:


    #include <stdio.h>
    
    intmain()
    {
        printf("hello Makefile!");
        sleep(3);
        return 0;
    }

Phenomenon:


    #include <stdio.h>
    
    intmain()
    {
        printf("hello Makefile!");
        fflush(stdout);
        sleep(3);
        return 0;
    }

Phenomenon:


Line buffering: flush the buffer when \n is encountered, and perform real I/O operations when newlines are encountered in input and output. At this time, the characters we input are first stored in the buffer, and the actual I/O operation is performed when the Enter key is pressed to change the line. The typical representative is that the keyboard input data and output information to the screen.

Progress bar code:

    #include <stdio.h>
    #include <unistd.h>
    int main(void)
    {
        char buf[102]="#";
        int i;
        char *r="-/|-\\|";
        for(i=1;i<=100;i++)
        {
            printf("\033[33m[%-100s][%c]\033[0m\r\r",buf,r[i%6]);
            buf[i]='#';
            fflush(stdout);
            usleep(100000);
        }
        printf("\n");
    
    }

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326974084&siteId=291194637