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

xml*_*lmx 62 c c++ io user-interface c++11

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

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

lee*_*mes 96

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

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;
Run Code Online (Sandbox Code Playgroud)

http://ideone.com/Yg8NKj

[>                                                                     ] 0 %
[===========>                                                          ] 15 %
[======================>                                               ] 31 %
[=================================>                                    ] 47 %
[============================================>                         ] 63 %
[========================================================>             ] 80 %
[===================================================================>  ] 96 %
Run Code Online (Sandbox Code Playgroud)

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

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

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

此外,当然可以printf在C中使用; 适应上面的代码应该是直截了当的.


Jam*_*ran 50

您可以使用"回车"(\ r)而不使用换行符(\n),并希望您的控制台做正确的事情.

  • +手动冲洗,否则不会立即显示,因为输出是缓冲的. (27认同)
  • @Ali 可能有一个相当于 W1ndOw$ 的东西,但我不知道。;) (2认同)

raz*_*zak 24

对于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);
}
Run Code Online (Sandbox Code Playgroud)

它将输出如下内容:

 75% [||||||||||||||||||||||||||||||||||||||||||               ]
Run Code Online (Sandbox Code Playgroud)

  • 这是迄今为止我发现的最简单和最好的解决方案 (3认同)

Joh*_*ela 13

看一下boost progress_display

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

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


Mat*_*ine 9

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

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

  • +手动冲洗,否则不会立即显示,因为输出是缓冲的. (4认同)
  • +''\ b'`用于将光标向左移动一个位置. (3认同)

Fla*_*Cat 6

我知道我回答这个问题有点晚了,但我做了一个简单的类,它完全符合你的要求。(请记住,我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
};
Run Code Online (Sandbox Code Playgroud)

关于如何使用的示例:

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;
}
Run Code Online (Sandbox Code Playgroud)

注意:我公开了所有类的字符串,以便可以轻松更改栏的外观。


A J*_*A J 5

另一种方法可能是显示“点”或您想要的任何字符。下面的代码将在 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;
}
Run Code Online (Sandbox Code Playgroud)