如何回滚线从cout? [英] How to rollback lines from cout?

查看:131
本文介绍了如何回滚线从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>

$ b

(或其他答案中提到的),但是大部分时间是不可见的(除非重定向到文件)。



< 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屋!

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