本文共 1057 字,大约阅读时间需要 3 分钟。
下面的代码动态刷新当前进度:
#include <iostream>#include <unistd.h>using namespace std;int main(){ int i = 0; printf("Progress ... %2d",i); for(i = 0; i < 100; i++){ printf("/b/b"); printf("%2d",i); fflush(stdout); sleep(1); } printf("/n"); return 0;}
运行过程中可能不会有任何输出,只在结束前输出“Progress ... 99“。
原因是输出一直在缓冲区中,并没有打印到屏幕。
fflush(stdout);
#include <iostream>#include <unistd.h>using namespace std;int main(){ int i = 0; printf("Progress ... %2d",i); for(i = 0; i < 100; i++){ printf("/b/b"); printf("%2d",i); //fflush(stdout);//<---- //cout.flush();//<--- cout << flush;//<--- sleep(1); } printf("/n"); return 0;}
转载地址:http://thntb.baihongyu.com/