C ++控制台进度指示器 [英] C++ Console Progress Indicator

查看:139
本文介绍了C ++控制台进度指示器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



对于正在执行的任务,实现基于控制台的进度指示器是一种简单的方法,但我无法预期。 p>当我在Clipper中编码时,我习惯这样做,它只是通过字符'/',' - ','\','|'迭代,并将它们放在同一个地方。 / p>

在C ++中做任何方式/ links / libs(或类似的东西)?



是* nix环境。



编辑




  • 将标题更改为更连贯和通用;

  • 添加目标环境。


解决方案

一个非常简单的方法是打印一个字符串后面跟一个'\r'字符。这是回车本身和在大多数控制台,它返回光标到行的开始,而不会向下移动。这允许你覆盖当前行。



如果你正在写stdout或cout或clog记住fflush或std :: flush流,使其输出行立即。如果你正在写stderr或cerr,那么流是无缓冲的,所有的输出都是即时的(效率低下)。



一个更复杂的方法是使用像curses的屏幕绘图库。 Windows控制台有一些其他的方式来设置他们的直接屏幕写作,但我不知道他们是什么。


What would be an easy way of implementing a console-based progress indicator for a task that's being executed, but I can't anticipate how much time it would take?

I used to do this back when I coded in Clipper, and it was only a matter of iterating through the chars '/', '-', '\', '|' and positioning them in the same place.

Any way / links / libs for doing that (or something similar) in C++?

The target for this is *nix environments.

Edits:

  • changed the title to be more coherent and generic;
  • added target environment.

解决方案

A very simple way to do it is to print out a string followed by a '\r' character. That is carriage return by itself and on most consoles, it returns the cursor to the beginning of the line without moving down. That allows you to overwrite the current line.

If you are writing to stdout or cout or clog remember to fflush or std::flush the stream to make it output the line immediately. If you are writing to stderr or cerr then the stream is unbuffered and all output is immediate (and inefficient).

A more complicated way to do it is to get into using a screen drawing library like curses. The Windows consoles have some other ways of setting them for direct screen writing but I don't know what they are.

这篇关于C ++控制台进度指示器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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