Clearing output of a terminal program in Linux C

前端 未结 3 764
栀梦
栀梦 2021-02-04 22:35

I want to clear the output of a C program produced with printf statements. I want to clear only one line, for example:

[source]

printf(\"AAAAAAAAAAAAAA\\         


        
3条回答
  •  迷失自我
    2021-02-04 23:11

    There're several ways to delete the DDDDDDDDDDDDDD

    1. Print backspace several times
    printf("\b");
    
    1. Print carriage-return and then print something to override the original line
    printf("\r");
    
    1. If you are in a newline. You may use terminal commands to move your cursor

    such as printf("\033[8;5Hhello"); // Move to (8, 5) and output hello

    Other commands:

    printf("\033[XA"); // Move up X lines;
    printf("\033[XB"); // Move down X lines;
    printf("\033[XC"); // Move right X column;
    printf("\033[XD"); // Move left X column;
    printf("\033[2J"); // Clear screen
    ...
    
    1. Don't forget ncurses

    It is the best ways to control the exact layout and format in a terminal

提交回复
热议问题