Modifying text in the terminal

后端 未结 4 1780
别跟我提以往
别跟我提以往 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:26

    There are a number of ways to do this, and depending on how much effort you want to put into it, you can do a lot of cool things with ascii text in a terminal window.

    1. Advanced: ncurses library

    2. Easier: ansi escape characters with printf or cout

    3. Easiest: As others have said, simply use \r for a carriage return without a linefeed.

    Edit: example of using the ESC sequence to go back two characters:

    #include 
    #define ESC char(0x1B)
    
    int main(){
      std::cout << "This will overwrite 'rs' in the following: characters" << ESC << "[2D" << "xx" << std::endl;
      return 0;
    }
    

提交回复
热议问题