Achieve progress bar program under Linux. Compiled by makefile. It is recommended to complete a self-colored progress bar.
fflush , in the realization of the need to complete each output using fflush (stdout) will refresh buffer so that the next output.
memset computer in C / C ++ language initialization function. The role is a piece of content in memory of all the specified value, this function is usually to do the initial work for the new memory applications.
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <string.h>
4 #define N 100
5
6 #define NONE "\e[0m"
7 #define RED "\e[0;31m"
8 #define BLUE "\e[0;34m"
9
10
11 int main()
12 {
13 char bar[N];
14 int per = 0;
15 //void *memset(void *s, int ch, size_t n);
16 //将s中当前位置后面的n个字节 (typedef unsigned int size_t )用 ch 替换并返回 s 。
17 //memset:作用是在一段内存块中填充某个给定的值,它是对较大的结构体或数组进行清零操作的一种最快方法。
18 //memset()函数原型是extern void *memset(void *buffer, int c, int count) buffer:为指针或是数组,c:是赋给buffer的值,count:是buffer的长度.
19 memset(bar,'\0',sizeof(bar));
20 while(per<N)
21 {
22 bar[per]= '-';
23 printf(RED"[%-100s][%d%%]\r"NONE,bar,per+1);
24 fflush(stdout);
25 per++;
26 usleep(100000);
27 }
28 printf("\n");
29 return 0;
30 }