如何回滚线从cout? [英] How to rollback lines from cout?
问题描述
我编码任务监控,使用cout更新任务的进度。我想在每行显示一个任务进度,因此我必须回滚控制台的几行。
我坚持几个,因为 \b
为一行执行作业,但不擦除行之间的 \\\
。
我试过 std :: cout.seekp(std :: cout.tellp() - str.length());
但 tellp()
返回-1(失败)。
c> cout < '\r'; 跳转到当前行的开头,但向上移动是系统特定的。对于Unix,请参阅 man termcap
和 man terminfo
(并搜索 cursor_up
)。在ANSI兼容的终端(例如在Unix上可用的大多数现代终端),这工作向上移动: cout< c>
< slang )库为Unix上的终端I / O提供了良好的抽象。
I'm coding a task monitoring, which updates tasks' progress using cout. I'd like to display one task progress per line, therefore I have to rollback several lines of the console.
I insist on "several" because \b
does the job for one line, but does not erase \n
between lines.
I tried std::cout.seekp(std::cout.tellp() - str.length());
but tellp()
returns -1 (failure).
You can do cout << '\r';
to jump to the beginning of the current line, but moving upwards is system-specific. For Unix, see man termcap
and man terminfo
(and search for cursor_up
). On ANSI-compatible terminals (such as most modern terminals available on Unix), this works to move up: cout << "\e[A";
.
Don't try seeking in cout
, it's unseekable most of the time (except when redirected to a file).
As mentioned in other answers, using the ncurses (or slang) library provides a good abstraction for terminal I/O on Unix.
这篇关于如何回滚线从cout?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!