Linux 進度條

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

int progress_bar(int precent)  
{
	if (precent < 0 || precent > 100)
	{
		return -1;
	}

	char str[32];
	int bar = sizeof(str) * precent / 100;	
	
	memset(str, ' ', sizeof(str));
	memset(str, '#', bar);
	
	fprintf(stdout, "%s|%d%%\r", str, precent); //實際就是轉移符\r的使用,\r的作用是返回至行首而不換行
	fflush(stdout); //一定要fflush,否則不會會因爲緩衝無法定時輸出。
	
	return 0;
}
  
int main()
{	
	int i = 0;
	for (i = 0; i <= 100; i++)
	{
		progress_bar(i);
		usleep(100*1000);
	}
	
	printf("\n");
	return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章