In Linux, console applications often display their progress by the similarity of this: [####___] 50% , while the inscription is “updated” in the same place in the terminal, rather than spamming in cmd, as does the count <<"сообщение"<<endl; in Windows. I look at such "spam" now, it would be better to have one "updated" line. I look at such "spam" now, it would be better to have one "updated" line

  • try \r , for example - Abyx
  • @Abyx nothing has changed. I can put anything at the beginning of the lines like this, but the lines themselves will be "spamming". and I want the new line to take the place of the old, pseudo-gui is. - Gleb
  • So it seems there are already answers to this question: cursor position for stdout ; SetConsoleCursorPosition - Daemon-5
  • 2
    '\ r' moves the cursor to the beginning of the line. Naturally, the lines should not be '\ n' - translation of lines. Since the characters of the previous output are not erased, the next output should not be shorter than the previous one, for example, you can stick gaps to the end. - Alex Titov
  • one
    By the way, any running lines are output in 'std :: cerr,', then 1) there will be no problems with resetting the buffer, which was written about in the answer, and 2) you can output (if useful) 'std :: cout' to redirect to a file, and the running line will still be on the screen. - Alex Titov

2 answers 2

Run the following code:

 #include <string> #include <iostream> #include <chrono> #include <thread> using namespace std; int main(int argc, const char * argv[]) { for(int i = 0;; ++i) { string s; for(int j = 0; j < 50; ++j) { s.push_back(j < i%50 ? '#' : ' '); } s.push_back('\r'); cout << s; this_thread::sleep_for(50ms); } } 

Perhaps if you do not have VC ++, you will also need to reset the cout buffer.

     #include<Windows.h> int main () { HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorPosition(hOut, {10,10}); //Эта функция устанавливает положение курсора, то есть можно перерисовать результат return 0; }