void progress_update(unsigned long total_max, unsigned long total_val, int child_max, int child_val, struct timeval *last_refresh )
{
double total_percent, child_percent;
int i;
char pstr[1024];
struct timeval *org_last_refresh = last_refresh;
if( total_max != total_val || child_max != child_val ){
struct timeval current_time;
gettimeofday(¤t_time, NULL);
if( current_time.tv_sec - org_last_refresh->tv_sec < 1 ){
return;
}
}
if( total_max < 1 )
return;
gettimeofday(org_last_refresh, NULL);
memset(pstr, 0, 1024);
// total progress
// xx.xx%
total_percent=100.00*total_val/total_max;
sprintf(pstr, "Total: %6.2f%s", total_percent, "%");
// [====== ]
strcat(pstr, "\t[");
for(i=0; i<total_percent/100*30; i++){
strcat( pstr, "=");
}
for(;i<30; i++){
strcat( pstr, " " );
}
strcat( pstr, "]" );
// child progress
child_percent=100.00*child_val/child_max;
sprintf(pstr, "%s\tChild: %6.2f%s", pstr, child_percent, "%");
// [====== ]
strcat(pstr, "\t[");
for(i=0; i<child_percent/100*30; i++){
strcat( pstr, "=");
}
for(;i<30; i++){
strcat( pstr, " " );
}
strcat( pstr, "]" );
// refresh screen
fflush(stdout);
for(i=0; i<(strlen(pstr)*1.5); i++){
printf("\b");
}
printf(pstr);
if( total_max == total_val && child_max == child_val )
printf("\n");
return;
}
C语言编写的进程进度条显示程序
最新推荐文章于 2022-10-11 12:26:29 发布