您的位置:首页 > 其它

终端同一行输出进度信息

2015-08-07 11:33 183 查看
直接贴代码:

1 #define MYSIZE 32*1024*1024

#include <iostream>
#include <iomanip>
#include <fstream>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>

using namespace std;

unsigned int My_buffer[MYSIZE];

string getFileName();

int main(int argc, char **argv)
{
//out current thread id;
cout<<getpid()<<endl;

struct timeval tpseed;
int n;
float process;
float rule;

cout<<"Data Buffer is Creating ...";
//clean output buffer
fflush(stdout);
//process step
rule=0.01;

for(n=0;n<MYSIZE;n++)
{
//get millionseconds
gettimeofday(&tpseed,NULL);
//get rand
srand(tpseed.tv_usec);
My_buffer
=(unsigned int)(1+(3000.0*rand())/(RAND_MAX+1.0));

process=n/(MYSIZE/100.0);

if(n%(MYSIZE/100)==0 || ((int)process)<10)
{
if(process>rule)
{
rule=rule+0.01;
//clean outside char
cout<<"\r                                                                           ";
/*output avaliable char
*\033[32;5m  set fonts properties
* \033[0m  set fonts nomal
*/
cout<<"\r\033[32;5m Data Buffer is Creating \033[31;1m"
<<setprecision(2)<<process<<"%\033[32;5m ...\033[0m";
//clean output buffer
fflush(stdout);
}
}
}
cout<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: