作者: zjujoe 转载请注明出处
Email:zjujoe@yahoo.com
BLOG:http://blog.youkuaiyun.com/zjujoe
最近看到一个工具在命令行下显示进度, 好奇它是如何实现的, 原来是打印时使用 /r 参数, 非常简单, 以前居然没有接触过!(还有以0开头的数字表示八进制,也是最近看代码才知道的)。 程序贴在这里, 以飨好奇者。
#include<stdio.h>
show_progress(unsignedlong total, unsigned long cur)
{
static unsigned long p = 0;
static const unsigned char w[] ="///-";
unsigned long val;
val = (cur * 100) / total;
if(++p == 3) p = 0;
printf("/r %4d%c%c", val, '%',w[p]);
}
intmain(void)
{
int i;
int j;
int k;
for (i=0; i <= 100; i++)
{
for (j=0; j <10000; j++)
for (k=0; k <10000; k++)
;
show_progress(100, i);
fflush(stdout);
}
printf("/n");
return 0;
}