<small id='4vpFy'></small><noframes id='4vpFy'>

<legend id='4vpFy'><style id='4vpFy'><dir id='4vpFy'><q id='4vpFy'></q></dir></style></legend>

  1. <i id='4vpFy'><tr id='4vpFy'><dt id='4vpFy'><q id='4vpFy'><span id='4vpFy'><b id='4vpFy'><form id='4vpFy'><ins id='4vpFy'></ins><ul id='4vpFy'></ul><sub id='4vpFy'></sub></form><legend id='4vpFy'></legend><bdo id='4vpFy'><pre id='4vpFy'><center id='4vpFy'></center></pre></bdo></b><th id='4vpFy'></th></span></q></dt></tr></i><div id='4vpFy'><tfoot id='4vpFy'></tfoot><dl id='4vpFy'><fieldset id='4vpFy'></fieldset></dl></div>

      • <bdo id='4vpFy'></bdo><ul id='4vpFy'></ul>

      <tfoot id='4vpFy'></tfoot>
    1. 如何在纯 C/C++ (cout/printf) 中显示进度指示器?

      How to display a progress indicator in pure C/C++ (cout/printf)?(如何在纯 C/C++ (cout/printf) 中显示进度指示器?)
        <tbody id='Nz9CZ'></tbody>
          <bdo id='Nz9CZ'></bdo><ul id='Nz9CZ'></ul>
        • <tfoot id='Nz9CZ'></tfoot>

                <small id='Nz9CZ'></small><noframes id='Nz9CZ'>

                <legend id='Nz9CZ'><style id='Nz9CZ'><dir id='Nz9CZ'><q id='Nz9CZ'></q></dir></style></legend>
                <i id='Nz9CZ'><tr id='Nz9CZ'><dt id='Nz9CZ'><q id='Nz9CZ'><span id='Nz9CZ'><b id='Nz9CZ'><form id='Nz9CZ'><ins id='Nz9CZ'></ins><ul id='Nz9CZ'></ul><sub id='Nz9CZ'></sub></form><legend id='Nz9CZ'></legend><bdo id='Nz9CZ'><pre id='Nz9CZ'><center id='Nz9CZ'></center></pre></bdo></b><th id='Nz9CZ'></th></span></q></dt></tr></i><div id='Nz9CZ'><tfoot id='Nz9CZ'></tfoot><dl id='Nz9CZ'><fieldset id='Nz9CZ'></fieldset></dl></div>
              1. 本文介绍了如何在纯 C/C++ (cout/printf) 中显示进度指示器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在用 C++ 编写一个控制台程序来下载一个大文件.我知道文件大小,并且我启动了一个工作线程来下载它.我想显示一个进度指示器,让它看起来更酷.

                I'm writing a console program in C++ to download a large file. I know the file size, and I start a work thread to download it. I want to show a progress indicator to make it look cooler.

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

                How can I display different strings at different times, but at the same position, in cout or 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) << " %
                ";
                    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 命令行中)它会打印在同一行.

                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.".

                此外,当然可以在 C 中使用 printf 来完成同样的操作;修改上面的代码应该很简单.

                Also, the same can of course be done using printf in C; adapting the code above should be straight-forward.

                这篇关于如何在纯 C/C++ (cout/printf) 中显示进度指示器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Is Type(::x); valid?(是类型(::x);有效的?)
                Difference between an inline function and static inline function(内联函数和静态内联函数的区别)
                Compilation fails randomly: quot;cannot open program databasequot;(编译随机失败:“无法打开程序数据库)
                Too many initializers error for a simple array in bcc32(bcc32 中的简单数组的初始值设定项过多错误)
                No Member named stoi in namespace std(命名空间 std 中没有名为 stoi 的成员)
                Error using a constexpr as a template parameter within the same class(在同一个类中使用 constexpr 作为模板参数时出错)

                  <small id='iT4DZ'></small><noframes id='iT4DZ'>

                  <legend id='iT4DZ'><style id='iT4DZ'><dir id='iT4DZ'><q id='iT4DZ'></q></dir></style></legend>
                    • <bdo id='iT4DZ'></bdo><ul id='iT4DZ'></ul>
                      <tfoot id='iT4DZ'></tfoot>

                          <i id='iT4DZ'><tr id='iT4DZ'><dt id='iT4DZ'><q id='iT4DZ'><span id='iT4DZ'><b id='iT4DZ'><form id='iT4DZ'><ins id='iT4DZ'></ins><ul id='iT4DZ'></ul><sub id='iT4DZ'></sub></form><legend id='iT4DZ'></legend><bdo id='iT4DZ'><pre id='iT4DZ'><center id='iT4DZ'></center></pre></bdo></b><th id='iT4DZ'></th></span></q></dt></tr></i><div id='iT4DZ'><tfoot id='iT4DZ'></tfoot><dl id='iT4DZ'><fieldset id='iT4DZ'></fieldset></dl></div>
                            <tbody id='iT4DZ'></tbody>