4

I would like to print a line, than delete it, than print another one in the same line. I would like this because I do not to loose or go to much back for getting the information that was printed before the loop where I print things like Processing file <X>. I know that it is possible, because I have seen stuffs like that, but I did not found the way to do it.

This is my code:

std::cout << "Print important information" << std::endl;
for (int i = 0; i < filesVec.size(); i++)
{
  std::cout << "Processing file " << i << std::endl;
  // processing file
}
std::cout << "Print another important information" << std::endl;

This prints me a lot of lines between the first and second important informations. I would like to print all the processing file X info to see that it is not getting stuck on some file. At the end I need the important info, so Is there a statement to do what I want?

thedarkside ofthemoon
  • 2,251
  • 6
  • 31
  • 48

1 Answers1

9

You can use a VT100 escape code to clear the line, followed by a carriage return \r. Most terminals, including xterm and the Visual Studio Community 2019 console, are VT100 aware. To make sure to print the line, you can end with a std::flush. After the end of the for loop you can follow the latest progress line with a newline and a completion report.

The end result will look something like:

std::cout << "Print important information" << std::endl;
for (int i = 0; i < filesVec.size(); i++)
{
  std::cout << "\33[2K\rProcessing file " << i << std::flush;
  // processing file
}
std::cout << std::endl; // Added to not overwrite the last status message.
std::cout << "Print another important information" << std::endl;

See also Erase the current printed console line

Fabio A. Correa
  • 1,968
  • 1
  • 17
  • 26
Mr. Llama
  • 20,202
  • 2
  • 62
  • 115
  • @thedarkside you'll have to fill the line with spaces, otherwise a new shorter line will intertwine with the end of the previous longer line – bolov Jul 01 '14 at 16:39
  • @bolov - Correct. However, in this situation, the value of `i` is guaranteed to be at least as long as the previous value. – Mr. Llama Jul 01 '14 at 16:40
  • I see the problem... Is there another way of cleaning the screen? – thedarkside ofthemoon Jul 01 '14 at 16:44
  • Could you elaborate on what you mean by that? – Mr. Llama Jul 01 '14 at 16:45
  • Yes, I do not like to have `Processing file BB.XAA.Y`, because of the length of the previous file – thedarkside ofthemoon Jul 01 '14 at 16:46
  • Another problem is that on kdevelop, ubuntu, in debug, it is not displaying until the end and then it is printing all on the same line and after that the important info at the end of that line... – thedarkside ofthemoon Jul 01 '14 at 16:48
  • 1
    The loop you posted in your question is printing the file number, not the file name. The file number is guaranteed to be at least as long as the previous value, so you won't have fragments left over. If you decide to print file names, you can simply add some space padding before the `\r` to overwrite the previous file name. – Mr. Llama Jul 01 '14 at 16:50
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/56607/discussion-between-mr-llama-and-thedarkside-ofthemoon). – Mr. Llama Jul 01 '14 at 16:51
  • For clearing the current line, refer to http://stackoverflow.com/a/1508589/2580849 and its comment. – NigoroJr May 12 '15 at 21:21