How to roll back lines from cout?

I encode task monitoring, which updates the progress of tasks using cout. I would like to display one task progress on each line, so I need to drop a few 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 (failed).

+10
c ++ cout
source share
6 answers

You can do cout << '\r'; to go to the beginning of the current line, but moving up is system dependent. For Unix, see man termcap and man terminfo (and find cursor_up ). On ANSI-compatible terminals (such as most modern terminals available on Unix) this works to move up: cout << "\e[A"; .

Do not try to search in cout , most of the time it cannot be found (except when it is redirected to a file).

As mentioned in other answers, using the ncurses library (or slang) provides a good abstraction for terminal I / O on Unix.

Instead of filling with spaces (which is error prone, since not every terminal is 80 characters wide), you can run \r + clr_eol : std::cout << "\r\e[K" << std::flush .

+20
source share

Use an output formatting library such as ncurses if you can; this greatly simplifies terminal manipulation.

+8
source share

Neither C nor C ++ defines anything like this. You need explicit terminal manipulation. On Unix, you can use curses . I don’t know what is there for Windows.

+5
source share

I know that this is an old post, but the accepted one does not apply to cases where cout is sent to a program or file, and this is the top of my search queries. The following will handle both tubular and non-pigmented stdout with slightly different behavior.

 #include <iostream> #include <functional> #include <stdio.h> #ifdef _WIN32 #include <io.h> #else #include <unistd.h> #define _isatty isatty #define _fileno fileno #endif const std::function<void(const size_t&)> progress_printer(_isatty(_fileno(stdout)) == 1 ? [](const size_t& i) { std::cout << "\rNumber " << i << std::flush; } : [](const size_t& i) { static std::ios::off_type last(-1); if(last != -1) std::cout.seekp(last, std::ios::beg); last = std::cout.tellp(); std::cout << "Number " << i << std::endl; } ); 

This is not tested on windows, but should work. What he does is determine if the file descriptor is or is tty. If so, then he simply writes '\ r' if pos has not changed since his last print or a new line. If it is not a newline, it searches for the last place after printing. A.

It behaves differently for files than for tty. For a file, if something outputs a stream between prints, it can overwrite some or all of what was written even after a newline. For ttys, it simply overwrites the characters at the beginning of the current line.

+2
source share

Hope this helps;) [This should work on Linux.]

 // "\e[0K" Clear line from cursor to the end cout << "\e[A\r\e[0K"<<what_you_want<<endl; 
0
source share

You can use the first system (""); because you can use \ e [A (Dev-C ++) or \ u001B [A (Visual Studio)

 #include <iostream> using namespace std; int main() { system(" "); string Input; do { cout << "[#][\e[s"; cin >> Input; cout << "[\e[u" << Input << "]"<<endl; } while (2==2); return 0; } 

enter image description here

0
source share

All Articles