【c++】打印進度條,Linux下進度條輸出

代碼:ios

#include <unistd.h>
#include <iostream>
#include <string>
#include <iomanip>
/*設置必備的頭文件*/
using namespace std;spa

void print_process(string strName, float fValue, bool flag = false)
{
if(0>fValue || fValue>1)
{
cout << "進度參數必須大於0且小於1" << endl;
return;
}

fValue = fValue*100;

cout<<setiosflags(ios::fixed)<<setprecision(2);  //設置出書的小數後精度爲2

string tag = "["+strName+"]"+"process:"+string((fValue/10), '*')+"[";
// flush擦除,\r定位到行首
cout << flush << '\r' << tag << fValue << "%]";
if(flag)
{
usleep(1000); //1000us
}
}ip

int main() {
for(int i = 0; i <=12345; i++) {
// flush擦除,\r定位到行首
float f = (float)i/12345.0;
print_process("mytest",f,true);
}
cout << endl;
cin.get();
return 0;
}ci

相關文章
相關標籤/搜索