简体   繁体   English

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

[英]Clearing output of a terminal program in Linux C

I want to clear the output of a C program produced with printf statements.我想清除用 printf 语句生成的 C 程序的输出。 I want to clear only one line, for example:我只想清除一行,例如:

[source] [来源]

printf("AAAAAAAAAAAAAA\n");
printf("BBBBBBBBBBBBBB\n");
printf("CCCCCCCCCCCCCC\n");
printf("DDDDDDDDDDDDDD\n");

[terminal] [终端]

AAAAAAAAAAAAAA
BBBBBBBBBBBBBB
CCCCCCCCCCCCCC
DDDDDDDDDDDDDD

[I hope] [我希望]

AAAAAAAAAAAAAA
BBBBBBBBBBBBBB
CCCCCCCCCCCCCC

I will "DDDDDDDDDDDDDD" line in write other string.我将在“DDDDDDDDDDDDDD”行中写入其他字符串。 I just want the above A, B, C sentences to left.我只想把上面的 A、B、C 句留下。 Only clear D sentences to change the other sentences, unconditionally output D sentences.只清除D句改变其他句,无条件输出D句。

How do I do this?我该怎么做呢?

There're several ways to delete the DDDDDDDDDDDDDD有几种方法可以删除 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比如printf("\\033[8;5Hhello"); // Move to (8, 5) and output hello 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不要忘记 ncurses

It is the best ways to control the exact layout and format in a terminal这是在终端中控制确切布局和格式的最佳方法

If you are using X-Term compatibles (Gnome Terminal included), then print the following如果您使用的是 X-Term 兼容软件(包括 Gnome 终端),请打印以下内容

printf("\033[2J");

or或者

cout << "\033[2J";

where \\033 is the escape character in ASCII and [2J is the specific action (clear).其中\\033ASCII 中的转义字符, [2J是特定操作(清除)。

I tried answering for what is best expected here.我试着回答这里最好的预期。

printf("AAAAAAAAAAAAAA");
printf("BBBBBBBBBBBBBB");
printf("CCCCCCCCCCCCCC");
//printf("DDDDDDDDDDDDDD");

comment the last line or delete if you dont want to display in terminal.如果您不想在终端中显示,请注释最后一行或删除。 printf("xxxx") is the statement used for printing output in terminal. printf("xxxx") 是用于在终端打印输出的语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM