最近实现了进度条,记录一下。
1. 建立工作目录 test,并进入
2. 建立proccess.h 实现代码如下:
<span style="font-size:18px;">#ifndef _PROCCESS_
#define _PROCCESS_
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#define _SIZE_ 103
void process();
#endif // !_PROCCESS_
</span>
3. 建立 proccess.c 实现代码如下:
<span style="font-size:18px;">#include "proccess.h"
void process()
{
int i = 1;
char str[_SIZE_];
memset(str, '\0', sizeof(str));
str[_SIZE_ - 1] = '\0';
str[_SIZE_ - 2] = ']';
str[0] = '[';
char index[5] = "-\\|/\0";
while (i <= 100)
{
str[i] = '#';
printf("%S[%d%%%c]\r", str, i, index[i % 4]);
fflush(stdout);
i++;
usleep(100000);
}
printf("\n");
}</span>
4.建立main.c 实现代码如下:
<span style="font-size:18px;">#include <stdio.h>
#include "proccess.h"
int main()
{
process();
return 0;
}</span>
5. 建立Malefile文件 代码如下:
<span style="font-size:18px;">MY_PATH=$(shell pwd)
TARGET=process
#CC=g++
CC=gcc
SRC=$(shell ls *.c)
OBJ_O=$(SRC:.c=.o)
INCLUDE=./
LINB=./
$(TARGET):$(OBJ_O
@echo "[$@] begin building...done"
@$(CC) -o $(TARGET) $(OBJ_O) -L$(LIB)
%.o:%.c
@echo "[$@] begin compling...done"
@$(CC) -c $< -I$(INCLUDE)
.PHONY:clean
clean:
@rm -rf *.o $(TARGET)</span>
效果: