我想打印一行,然后删除它,然后在同一行中打印另一行。我想要这个,因为我不会丢失或返回太多信息来获取在循环之前打印的信息,例如Processing file <X>
。我知道这是可能的,因为我见过类似的东西,但我没有找到方法。
这是我的代码:
std::cout << "Print important information" << std::endl;
for (int i = 0; i < filesVec.size(); i++)
{
std::cout << "Processing file " << i << std::endl;
// processing file
}
std::cout << "Print another important information" << std::endl;
这会在第一个和第二个重要信息之间打印很多行。我想打印所有processing file X
信息以查看它没有卡在某些文件上。最后我需要重要的信息,所以有一个声明可以做我想做的事吗?
您可以使用VT100 转义码 http://www.climagic.org/mirrors/VT100_Escape_Codes.html清除该行,然后回车\r
。大多数终端(包括 xterm 和 Visual Studio Community 2019 控制台)都支持 VT100。为了确保打印该行,您可以以std::flush
。 for 循环结束后,您可以使用换行符和完成报告跟踪最新进度线。
最终结果将类似于:
std::cout << "Print important information" << std::endl;
for (int i = 0; i < filesVec.size(); i++)
{
std::cout << "\33[2K\rProcessing file " << i << std::flush;
// processing file
}
std::cout << std::endl; // Added to not overwrite the last status message.
std::cout << "Print another important information" << std::endl;
也可以看看擦除当前打印的控制台行 https://stackoverflow.com/questions/1508490/erase-the-current-printed-console-line/1508589#1508589
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)