<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) 中显示进度指示器?

      时间:2023-10-18
        <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) 中显示进度指示器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:使用 ifstream 读取文件 下一篇:在 Windows 中获取用户临时文件夹路径

                相关文章

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