Modifying text in the terminal

后端 未结 4 1820
别跟我提以往
别跟我提以往 2021-01-14 08:54

Is it possible to modify text I printed to the terminal without clearing the screen?

For instance, if I\'m showing progress of something in percentage, can I modify

4条回答
  •  深忆病人
    2021-01-14 09:15

    A very simple way to do it is to print out a string followed by a '\r' character. That is carriage return by itself and on most consoles, it returns the cursor to the beginning of the line without moving down. That allows you to overwrite the current line.

    If you are writing to stdout or cout or clog remember to fflush or std::flush the stream to make it output the line immediately. If you are writing to stderr or cerr then the stream is unbuffered and all output is immediate (and inefficient).

    A more complicated way to do it is to get into using a screen drawing library like curses.

提交回复
热议问题