先把重要结论放这:
详情见我的另一篇文章:
- #include
- #include
-
- int main()
- {
- int i = 10;
- while(i)
- {
- printf("%-2d\r", i);
- fflush(stdout);
- sleep(1);
- i--;
- }
- return 0;
- }
%-2d,保留两位数且向左对齐
- #include
- #include
- #include
-
- #define MAX 101
- #define LABEL '='
-
- int main()
- {
- char bar[MAX];
- memset(bar, '\0', sizeof(bar));
- int count = 101;
- while(count--)
- {
- bar[100 - count] = LABEL;
- usleep(100000);
- printf("%s\r", bar);
- fflush(stdout);
- }
- printf("\n");
- return 0;
- }
- #include"processbar.h"
-
- int main()
- {
- processbar();
- return 0;
- }
- #pragma once
-
- #include
-
- #define NUM 101
- #define Body '='
- #define Head '>'
-
- void processbar();
- #include"processbar.h"
- #include
- #include
-
- const char *loading = "|/-\\";
-
- void processbar()
- {
- char bar[NUM];
- int n = strlen(loading);
- memset(bar, '\0', sizeof(bar));
- int cnt = 0;
- while(cnt <= 100)
- {
- bar[cnt++] = Body;
- if(cnt < 100)
- bar[cnt] = Head;
- printf("[%-101s][%3d%%][%c]\r", bar, cnt - 1, loading[cnt%n]);
- fflush(stdout);
- usleep(100000);
- }
- printf("\n");
- }
- processbar:main.o processbar.o
- gcc -o $@ $^
-
- main.o:main.c
- gcc -c main.c
-
- processbar.o:processbar.c
- gcc -c processbar.c
-
- .PHONY:clean
-
- clean:
- rm processbar main.o processbar.o
- #include"processbar.h"
-
- int main()
- {
- processbar();
-
- return 0;
- }
- #pragma once
-
- #include
- #include
- #include
- #include
- #include
-
- #define NUM 101
- #define Body '='
- #define Head '>'
- #define FILESIZE 1024*1024*1024
-
- void processbar();
- #include"processbar.h"
-
- char bar[NUM];
- const char *label = "|/-\\";
-
- void processbar()
- {
- int total = FILESIZE;
- int num = 0;
- srand(time(NULL)^1023);
- memset(bar, '\0', sizeof(bar));
- while(total)
- {
- usleep(100000);
- int one = rand()%(1024*1024*50);
- total -= one;
- if(total < 0) total = 0;
- double rate = ((FILESIZE - total)*1.0 / (FILESIZE)) * 100.0;
- num++; num %= 4;
- memset(bar, Body, sizeof(char)*((int)rate + 1));
- if((int)rate < 100) bar[(int)rate] = Head;
- printf("[%-101s][%6.2f%%][%c]\r", bar, rate, label[num]);
- fflush(stdout);
- }
- printf("\n");
- }
- processbar:main.o processbar.o
- gcc -o $@ $^
-
- main.o:main.c
- gcc -c main.c
-
- processbar.o:processbar.c
- gcc -c processbar.c
-
- .PHONY:clean
-
- clean:
- rm processbar main.o processbar.o