清除终端程序Linux C/ c++的输出。

时间:2022-05-11 10:21:10

I'm interested in clearing the output of a C program produced with printf statements, multiple lines long.

我感兴趣的是清除用printf语句生成的C程序的输出,多行长。

My initial guess was to use

我最初的猜测是使用。

 printf("output1\n");
 printf("output2\n");
 rewind(stdout);
 printf("output3\n");
 printf("output4\n");

but this produces

但这生产

 output1
 output2
 output3
 output4

I was hoping it would produce

我希望它能产生。

 output3
 output4

Does anyone know how to get the latter result?

有人知道如何得到后一个结果吗?

7 个解决方案

#1


22  

You can have the desired result both for terminal and pipes if you remember to remove the control characters as well. This is hardcoded for two lines.

如果您还记得删除控制字符,那么您可以在终端和管道中得到所需的结果。这是两行代码的硬编码。

#include <stdio.h>

int
main ()
{
    fputs("output1\n",stdout);
    fputs("output2\n",stdout);
    fputs("\033[A\033[2K\033[A\033[2K",stdout);
    rewind(stdout);
    ftruncate(1,0); /* you probably want this as well */
    fputs("output3\n",stdout);
    fputs("output4\n",stdout);
    return 0;
}

#2


18  

Most terminals support ANSI escape codes. You can use a J (with parameter 2) to clear the screen and an H (with parameters 1,1) to reset the cursor to the top-left:

大多数终端支持ANSI转义代码。您可以使用J(带有参数2)来清除屏幕和H(带有参数1,1)将光标重置到左上角:

printf("\033[2J\033[1;1H");

Alternatively, a more portable solution would be to use a library such as ncurses, which abstracts away the terminal-specific details.

另外,一个更便携的解决方案是使用像ncurses这样的库,它可以抽象出特定于终端的细节。

#3


17  

Once you print something to the terminal you can't easily remove it. You can clear the screen but exactly how to do that depends on the terminal type, and clearing the screen will remove all of the text on the screen not just what you printed.

一旦你打印了一些东西到终端,你就不能轻易地删除它。您可以清除屏幕,但具体如何操作取决于终端类型,而清除屏幕将删除屏幕上的所有文本,而不只是您打印的内容。

If you really want fine control over the screen output use a library like ncurses.

如果你真的想要对屏幕输出有很好的控制,可以使用像ncurses这样的库。

#4


4  

As far as C is concerned, stdout is nothing more than a byte stream. That stream could be attached to a CRT (or flat screen), or it could be attached to a hardcopy device like a teletype or even a sheet-fed printer. Calling rewind on the stream won't necessarily be reflected on the output device, because it may not make any sense in context of that device; think about what rewinding would mean on a hardcopy terminal or a sheet-fed printer.

就C而言,stdout不过是一个字节流。这条流可以连接到一个CRT(或平板屏幕),或者它可以连接到一个硬拷贝设备,比如电传打字机,甚至是一个单张纸的打印机。在流上调用rewind并不一定反映在输出设备上,因为它在该设备的上下文中可能没有任何意义;想一想,在硬拷贝终端或单张纸打印机上,重卷意味着什么。

C does not offer any built-in support for display management, so you'll have to use a third-party library like ncurses.

C不提供任何对显示管理的内置支持,因此您必须使用像ncurses这样的第三方库。

#5


4  

You can also try something like this, which clears the entire screen:

你也可以尝试这样的方法,它可以清除整个屏幕:

printf("\033[2J\033[1;1H");

You can include \033[1;1H to make sure if \033[2J does not move the cursor in the upper left corner.

您可以包括\033[1;1H,以确保\033[2J不将光标移到左上角。

More specifically:

更具体地说:

  • 033 is the octal of ESC
  • 033是ESC的八进制。
  • 2J is for clearing the entire console/terminal screen (and moves cursor to upper left on DOS ANSI.SYS)
  • 2J是用来清除整个控制台/终端屏幕(并将光标移动到DOS ANSI.SYS上的左上角)。
  • 1;1H moves the cursor to row 1 and column 1
  • 1 . 1H将光标移动到第1行和第1列。

#6


3  

One way, is to do a exec('clear').

一种方法是执行exec('clear')。

#7


2  

In fact, when you capture/redirect your stdout (./program > output.file), there is no way how to remove contents of that file, even printf("\033[2J\033[1;1H"); just adds this sequence of characters into it.

实际上,当您捕获/重定向您的stdout(。/program > output.file,没有办法删除该文件的内容,甚至printf(“\033[2J\033[1;1H]”);只是把这一系列的字符添加进去。

#1


22  

You can have the desired result both for terminal and pipes if you remember to remove the control characters as well. This is hardcoded for two lines.

如果您还记得删除控制字符,那么您可以在终端和管道中得到所需的结果。这是两行代码的硬编码。

#include <stdio.h>

int
main ()
{
    fputs("output1\n",stdout);
    fputs("output2\n",stdout);
    fputs("\033[A\033[2K\033[A\033[2K",stdout);
    rewind(stdout);
    ftruncate(1,0); /* you probably want this as well */
    fputs("output3\n",stdout);
    fputs("output4\n",stdout);
    return 0;
}

#2


18  

Most terminals support ANSI escape codes. You can use a J (with parameter 2) to clear the screen and an H (with parameters 1,1) to reset the cursor to the top-left:

大多数终端支持ANSI转义代码。您可以使用J(带有参数2)来清除屏幕和H(带有参数1,1)将光标重置到左上角:

printf("\033[2J\033[1;1H");

Alternatively, a more portable solution would be to use a library such as ncurses, which abstracts away the terminal-specific details.

另外,一个更便携的解决方案是使用像ncurses这样的库,它可以抽象出特定于终端的细节。

#3


17  

Once you print something to the terminal you can't easily remove it. You can clear the screen but exactly how to do that depends on the terminal type, and clearing the screen will remove all of the text on the screen not just what you printed.

一旦你打印了一些东西到终端,你就不能轻易地删除它。您可以清除屏幕,但具体如何操作取决于终端类型,而清除屏幕将删除屏幕上的所有文本,而不只是您打印的内容。

If you really want fine control over the screen output use a library like ncurses.

如果你真的想要对屏幕输出有很好的控制,可以使用像ncurses这样的库。

#4


4  

As far as C is concerned, stdout is nothing more than a byte stream. That stream could be attached to a CRT (or flat screen), or it could be attached to a hardcopy device like a teletype or even a sheet-fed printer. Calling rewind on the stream won't necessarily be reflected on the output device, because it may not make any sense in context of that device; think about what rewinding would mean on a hardcopy terminal or a sheet-fed printer.

就C而言,stdout不过是一个字节流。这条流可以连接到一个CRT(或平板屏幕),或者它可以连接到一个硬拷贝设备,比如电传打字机,甚至是一个单张纸的打印机。在流上调用rewind并不一定反映在输出设备上,因为它在该设备的上下文中可能没有任何意义;想一想,在硬拷贝终端或单张纸打印机上,重卷意味着什么。

C does not offer any built-in support for display management, so you'll have to use a third-party library like ncurses.

C不提供任何对显示管理的内置支持,因此您必须使用像ncurses这样的第三方库。

#5


4  

You can also try something like this, which clears the entire screen:

你也可以尝试这样的方法,它可以清除整个屏幕:

printf("\033[2J\033[1;1H");

You can include \033[1;1H to make sure if \033[2J does not move the cursor in the upper left corner.

您可以包括\033[1;1H,以确保\033[2J不将光标移到左上角。

More specifically:

更具体地说:

  • 033 is the octal of ESC
  • 033是ESC的八进制。
  • 2J is for clearing the entire console/terminal screen (and moves cursor to upper left on DOS ANSI.SYS)
  • 2J是用来清除整个控制台/终端屏幕(并将光标移动到DOS ANSI.SYS上的左上角)。
  • 1;1H moves the cursor to row 1 and column 1
  • 1 . 1H将光标移动到第1行和第1列。

#6


3  

One way, is to do a exec('clear').

一种方法是执行exec('clear')。

#7


2  

In fact, when you capture/redirect your stdout (./program > output.file), there is no way how to remove contents of that file, even printf("\033[2J\033[1;1H"); just adds this sequence of characters into it.

实际上,当您捕获/重定向您的stdout(。/program > output.file,没有办法删除该文件的内容,甚至printf(“\033[2J\033[1;1H]”);只是把这一系列的字符添加进去。