我正在用 C++ 编写一个控制台程序来下载一个大文件。我知道文件大小,我启动了一个工作线程来下载它。我想显示一个进度指示器,让它看起来更酷。
如何在 cout 或 printf 中在不同时间但在同一位置显示不同的字符串?
我正在用 C++ 编写一个控制台程序来下载一个大文件。我知道文件大小,我启动了一个工作线程来下载它。我想显示一个进度指示器,让它看起来更酷。
如何在 cout 或 printf 中在不同时间但在同一位置显示不同的字符串?
With a fixed width of your output, use something like the following:
float progress = 0.0;
while (progress < 1.0) {
int barWidth = 70;
std::cout << "[";
int pos = barWidth * progress;
for (int i = 0; i < barWidth; ++i) {
if (i < pos) std::cout << "=";
else if (i == pos) std::cout << ">";
else std::cout << " ";
}
std::cout << "] " << int(progress * 100.0) << " %\r";
std::cout.flush();
progress += 0.16; // for demonstration only
}
std::cout << std::endl;
[> ] 0 %
[===========> ] 15 %
[======================> ] 31 %
[=================================> ] 47 %
[============================================> ] 63 %
[========================================================> ] 80 %
[===================================================================> ] 96 %
Note that this output is shown one line below each other, but in a terminal emulator (I think also in Windows command line) it will be printed on the same line.
At the very end, don't forget to print a newline before printing more stuff.
If you want to remove the bar at the end, you have to overwrite it with spaces, to print something shorter like for example "Done."
.
Also, the same can of course be done using printf
in C; adapting the code above should be straight-forward.
您可以使用没有换行符 (\n) 的“回车”(\r),并希望您的控制台做正确的事情。
对于C
具有可调节进度条宽度的解决方案,您可以使用以下内容:
#define PBSTR "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||"
#define PBWIDTH 60
void printProgress(double percentage) {
int val = (int) (percentage * 100);
int lpad = (int) (percentage * PBWIDTH);
int rpad = PBWIDTH - lpad;
printf("\r%3d%% [%.*s%*s]", val, lpad, PBSTR, rpad, "");
fflush(stdout);
}
它将输出如下内容:
75% [|||||||||||||||||||||||||||||||||||||||||| ]
看看 boost progress_display
http://www.boost.org/doc/libs/1_52_0/libs/timer/doc/original_timer.html#Class%20progress_display
我认为它可能会满足您的需求,并且我相信它只是一个标头库,因此无需链接
您可以打印回车符 ( \r
) 以将输出“光标”移回当前行的开头。
对于更复杂的方法,请查看 ncurses(基于控制台文本的界面的 API)之类的东西。
我知道我回答这个问题有点晚了,但我做了一个简单的课程,完全符合你的要求。(请记住,我using namespace std;
在此之前写过。):
class pBar {
public:
void update(double newProgress) {
currentProgress += newProgress;
amountOfFiller = (int)((currentProgress / neededProgress)*(double)pBarLength);
}
void print() {
currUpdateVal %= pBarUpdater.length();
cout << "\r" //Bring cursor to start of line
<< firstPartOfpBar; //Print out first part of pBar
for (int a = 0; a < amountOfFiller; a++) { //Print out current progress
cout << pBarFiller;
}
cout << pBarUpdater[currUpdateVal];
for (int b = 0; b < pBarLength - amountOfFiller; b++) { //Print out spaces
cout << " ";
}
cout << lastPartOfpBar //Print out last part of progress bar
<< " (" << (int)(100*(currentProgress/neededProgress)) << "%)" //This just prints out the percent
<< flush;
currUpdateVal += 1;
}
std::string firstPartOfpBar = "[", //Change these at will (that is why I made them public)
lastPartOfpBar = "]",
pBarFiller = "|",
pBarUpdater = "/-\\|";
private:
int amountOfFiller,
pBarLength = 50, //I would recommend NOT changing this
currUpdateVal = 0; //Do not change
double currentProgress = 0, //Do not change
neededProgress = 100; //I would recommend NOT changing this
};
如何使用的示例:
int main() {
//Setup:
pBar bar;
//Main loop:
for (int i = 0; i < 100; i++) { //This can be any loop, but I just made this as an example
//Update pBar:
bar.update(1); //How much new progress was added (only needed when new progress was added)
//Print pBar:
bar.print(); //This should be called more frequently than it is in this demo (you'll have to see what looks best for your program)
sleep(1);
}
cout << endl;
return 0;
}
注意:我公开了所有类的字符串,以便可以轻松更改栏的外观。
另一种方法可能是显示“点”或您想要的任何字符。下面的代码将每隔 1 秒将进度指示器 [加载的排序...] 打印为点。
PS:我在这里使用睡眠。如果担心性能,请三思。
#include<iostream>
using namespace std;
int main()
{
int count = 0;
cout << "Will load in 10 Sec " << endl << "Loading ";
for(count;count < 10; ++count){
cout << ". " ;
fflush(stdout);
sleep(1);
}
cout << endl << "Done" <<endl;
return 0;
}
这是我做的一个简单的:
#include <iostream>
#include <windows.h>
using namespace std;
int barl = 20;
int main() {
system("color 0e");
cout << "[";
for (int i = 0; i < barl; i++) {
Sleep(100);
cout << ":";
}
cout << "]";
}
可能这段代码会帮助你 -
#include <iostream>
#include <string>
#include <thread>
#include <chrono>
#include <cmath>
using namespace std;
void show_progress_bar(int time, const std::string &message, char symbol)
{
std::string progress_bar;
const double progress_level = 1.42;
std::cout << message << "\n\n";
for (double percentage = 0; percentage <= 100; percentage += progress_level)
{
progress_bar.insert(0, 1, symbol);
std::cout << "\r [" << std::ceil(percentage) << '%' << "] " << progress_bar;
std::this_thread::sleep_for(std::chrono::milliseconds(time));
}
std::cout << "\n\n";
}
int main()
{
show_progress_bar(100, "progress" , '#');
}