如何在不重新打印的情况下更新终端中打印的消息 [英] How to update a printed message in terminal without reprinting

查看:24
本文介绍了如何在不重新打印的情况下更新终端中打印的消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的终端应用程序制作一个进度条,类似于:

I want to make a progress bar for my terminal application that would work something like:

 [XXXXXXX       ] 

这将给出在过程完成之前还剩下多少时间的视觉指示.

which would give a visual indication of how much time there is left before the process completes.

我知道我可以通过将它们添加到字符串然后简单的 printf 来打印越来越多的 X,但这看起来像:

I know I can do something like printing more and more X's by adding them to the string and then simply printf, but that would look like:

 [XXXXXXX       ] 
 [XXXXXXXX      ] 
 [XXXXXXXXX     ] 
 [XXXXXXXXXX    ] 

或类似的东西(显然您可以使用间距.)但这在视觉上并不美观.有没有办法在不重新打印的情况下用新文本更新终端中的打印文本?这都是在linux下,c++.

or something like that (obviously you can play with the spacing.) But this is not visually aesthetic. Is there a way to update the printed text in a terminal with new text without reprinting? This is all under linux, c++.

推荐答案

在打印新版本"时尝试使用 而不是 .

try using instead of when printing the new "version".

for(int i=0;i<=100;++i) printf("
[%3d%%]",i);
printf("
");

这篇关于如何在不重新打印的情况下更新终端中打印的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆