如何在终端中更新打印的邮件而不重新打印(Linux) [英] How to update a printed message in terminal without reprinting (Linux)

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

问题描述

我想为我的终端应用程序创建一个进度条,它可以工作:

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.

我知道我可以通过将更多的X添加到字符串然后只是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++.

推荐答案

尝试使用 \r

try using \r instead of \n when printing the new "version".

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

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

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