Undo a newline (\n) printed to command line

后端 未结 3 1658
暗喜
暗喜 2020-12-17 18:39
printf(\"Error %d\\n\", 1);
printf(\"\\nStatus: %d%%\", 50);

prints

Error 1

Status: 50%

In this set up, is ther

相关标签:
3条回答
  • 2020-12-17 18:46

    What @Ryan said.

    Explanation why: stdout is some abstract stream that doesn't have to be the terminal. It may be a file, a pipe, a socket, a printer, a text to speech device or whatever. In many cases there is no sense to what you asked to do. Hence you need some library that works with the terminal specifically.

    0 讨论(0)
  • 2020-12-17 18:49

    You could use ANSI Escapesequences to move your "cursor" one line up:

    void cursorOnLineUp(void) { printf("\033[1A"); }
    

    Or set it to a specific position:

    void setCursor(int column, int row) { printf("\033[%d;%dH", row, column) }
    

    Haven't tried it for C++, but succesfully used it for a simple game in ANSI-C!

    0 讨论(0)
  • 2020-12-17 18:50

    Sorry, you cannot.

    But you may issue system calls to clear the whole screen instead, like system("clear") (OS-dependent).

    Or use ncurses just as Kos mentioned in the comment.

    0 讨论(0)
提交回复
热议问题