Rewind std :: cout to return to the beginning of a line

I am writing a command line tool for Mac OS X that processes a bunch of files. I would like to show the user the current file being processed, but I do not want bazillion files to pollute the terminal window.

Instead, I would like to use one line to output the file path, and then reuse this line for the next file. Is there a character (or some other code) to output to std::cout to accomplish this?

Also, if I wanted to redirect this tool to Windows, would the solution be the same for both platforms?

+16
c ++ terminal stdout macos output-formatting
Jun 16 '10 at 23:46
source share
3 answers

"\ r" should work for both Windows and Mac OS X.

Something like:

 std::cout << "will not see this\rwill see this" << std::flush; std::cout << std::endl; // all done 
+16
Jun 16 '10 at 23:49
source share

I don’t have access to the Mac, but from a clean console perspective, this will largely depend on how it handles carriage return and line feed characters. If you can literally send this or that console, you only want to send a carriage return.

I'm sure the Mac handles both carriage return and line feeds differently than * nix and windows.

If you are looking for in-place updates (for example, overwrite the current line), I would recommend looking at curses lib. This should provide a platform-independent means of doing what you are looking for. (because, even using standard C ++, there is no platform-independent tool for what you ask for).

+1
Jun 16 2018-10-16T00:
source share

std :: cout interprets "\ r" as returning to the beginning of the line, if you do not want to add "<<endl" each time, use "\ n"

std::cout << "this will work!\nSee... a new line!" << std::endl;

-2
Nov 13 '14 at 13:34
source share



All Articles