How to update a printed message in terminal without reprinting How to update a printed message in terminal without reprinting linux linux

How to update a printed message in terminal without reprinting


try using \r instead of \n when printing the new "version".

for(int i=0;i<=100;++i) printf("\r[%3d%%]",i);printf("\n");


I'd say that a library like ncurses would be used to such things. curses helps move the cursor around the screen and draw text and such.

NCurses


Something like this:

std::stringstream out;for (int i = 0; i< 10; i++){  out << "X";  cout << "\r" << "[" << out.str() << "]";}

The sneaky bit is the carriage return character "\r" which causes the cursor to move to the start of the line without going down to the next line.