在Linux C中清除终端程序的输出

Nat*_*obs 2 c unix linux terminal clear

我想清除使用printf语句生成的C程序的输出.我想只清除一行,例如:

[资源]

printf("AAAAAAAAAAAAAA\n");
printf("BBBBBBBBBBBBBB\n");
printf("CCCCCCCCCCCCCC\n");
printf("DDDDDDDDDDDDDD\n");
Run Code Online (Sandbox Code Playgroud)

[终奌站]

AAAAAAAAAAAAAA
BBBBBBBBBBBBBB
CCCCCCCCCCCCCC
DDDDDDDDDDDDDD
Run Code Online (Sandbox Code Playgroud)

[我希望]

AAAAAAAAAAAAAA
BBBBBBBBBBBBBB
CCCCCCCCCCCCCC
Run Code Online (Sandbox Code Playgroud)

我将"DDDDDDDDDDDDDD"行写入其他字符串.我只想要留下上面的A,B,C语句.只有明确的D句改变其他句子,无条件输出D句.

我该怎么做呢?

Nar*_*uil 12

有几种方法可以删除DDDDDDDDDDDDD

1.打印退格数次

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

2.打印回车然后打印一些东西以覆盖原始行

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

3.如果你在换行.您可以使用终端命令移动光标

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

其他命令:

printf("\b");
Run Code Online (Sandbox Code Playgroud)

别忘了ncurses

这是控制终端中确切布局和格式的最佳方法