IT-Swarm.Net

如何在纯C/C++(cout/printf)中显示进度指示器?

我正在用C++编写一个控制台程序来下载一个大文件。我知道文件大小,我开始下载工作线程。我想显示一个进度指示器,使其看起来更酷。

如何在cout或printf中的不同时间,但在同一位置显示不同的字符串?

60
xmllmx

您可以使用没有换行符(\ n)的“回车”(\ r\n),并希望您的控制台做正确的事情。

45
James Curran

使用固定的输出宽度,使用以下内容:

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;

http://ideone.com/Yg8NKj

[>                                                                     ] 0 %
[===========>                                                          ] 15 %
[======================>                                               ] 31 %
[=================================>                                    ] 47 %
[============================================>                         ] 63 %
[========================================================>             ] 80 %
[===================================================================>  ] 96 %

请注意,此输出是显示彼此相差一行,但在终端模拟器中(我认为也在Windows命令行中)将打印在同一行上

在最后,不要忘记在打印更多东西之前打印换行符。

如果要删除末尾的栏,则必须用空格覆盖它,以打印更短的内容,例如"Done."

此外,当然可以使用C中的printf来完成相同的操作;适应上面的代码应该是直截了当的。

95
leemes

对于具有可调节进度条宽度的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% [||||||||||||||||||||||||||||||||||||||||||               ]
20
razzak

看一下boost progress_display

http://www.boost.org/doc/libs/1_52_0/libs/timer/doc/original_timer.html#Class%20progress_display

我认为它可以做你需要的东西,我相信它只是一个标题库,所以无需链接

13
John Bandela

您可以打印回车符(\r)将输出“光标”移回当前行的开头。

有关更复杂的方法,请查看ncurses(基于控制台文本的界面的API)之类的内容。

8
Matt Kline

我知道我回答这个问题有点晚了,但是我做了 一个简单的类,它完全符合你想要的。 (请记住我之前写了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;
}

注意:我公开了所有类的字符串,因此可以轻松更改条形图的外观。

3
Flare Cat

另一种方法可能是显示“圆点”或任何你想要的角色。下面的代码将在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;
}
3
A J

这是我做的一个简单的:

#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 << "]";
}
0
Kugel Blitz