C语言 在基于 UNIX 的系统上使用 C 和 C++ 清除屏幕?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17271576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:46:20  来源:igfitidea点击:

Clear screen in C and C++ on UNIX-based system?

cunixscreencls

提问by Shar

I want to know: how to clean screen on an UNIX-based system? I searched on the Internet, but I've just found how to do it on Windows: system("CLS") I don't want exactly to clean cpmpletely the screen, but I want to open a "new page", such as in the NANO and VI editors. Thanks

我想知道:如何在基于 UNIX 的系统上清理屏幕?我在互联网上搜索,但我刚刚发现如何在 Windows 上执行此操作: system("CLS") 我不想完全清理屏幕,但我想打开一个“新页面”,例如在 NANO 和 VI 编辑器中。谢谢

回答by David Ranieri

Maybe you can make use of escape codes

也许你可以使用转义码

#include <stdio.h>

#define clear() printf("3[H3[J")

int main(void)
{
    clear();
    return 0;
}

But keep in mind that this method is not compatible with all terminals

但请记住,此方法并非与所有终端都兼容

回答by Shar

You can use the following code which use termcap for clear screen. (don't forget to link with the library)

您可以使用以下代码,它使用 termcap 来清除屏幕。(不要忘记与图书馆链接)

#include <stdio.h>
#include <stdlib.h>
#include <termcap.h>

void clear_screen()
{
char buf[1024];
char *str;

tgetent(buf, getenv("TERM"));
str = tgetstr("cl", NULL);
fputs(str, stdout);
} 

回答by paxdiablo

Portable UNIX code should be using the terminfo database for all cursor and screen manipulation. This is what libraries like cursesuses to achieve its effects like windowing and so forth.

便携式 UNIX 代码应该使用 terminfo 数据库进行所有光标和屏幕操作。这就是库喜欢curses用来实现窗口等效果的东西。

The terminfo database maintains a list of capabailities (like clearwhich is what you would use to clear the screen and send the cursor to the top). It maintains such capabilities for a widerange of devices so that you don't have to worry about whether you're using a Linux console or a (very dated) VT52 terminal.

terminfo 数据库维护了一个功能列表(就像clear您用来清除屏幕并将光标发送到顶部的列表一样)。它为各种设备提供了此类功能,因此您不必担心您使用的是 Linux 控制台还是(非常过时的)VT52 终端。

As to how you getthe character streams for certain operations, you can choose the time-honored but rather horrible method of just using systemto do it:

至于如何获取某些操作的字符流,您可以选择历史悠久但相当可怕的方法system来执行它:

system ("tput clear");

Or you can capture the output of that command to a buffer so later use involve only outputting the characters rather than re-running the command:

或者您可以将该命令的输出捕获到缓冲区,以便以后使用仅涉及输出字符而不是重新运行命令:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

static char scrTxtCls[20]; static size_t scrSzCls;

// Do this once.

FILE *fp = popen ("tput clear", "r");
scrSzCls = fread (scrTxtCls, 1, sizeof(scrTxtCls), fp);
pclose (fp);
if (scrSzCls == sizeof(scrTxtCls)) {
    actIntelligently ("you may want to increase buffer size");
}

// Do this whenever you want to clear the screen.

write (1, cls, clssz);

Or, you can link with ncursesand use its API to get whatever capabilities you want, though this might drag in quite a bit of stuff for something as simple as clearing the screen. Still, it's an option to be considered seriously since it gives you a lot more flexibility.

或者,您可以链接ncurses并使用它的 API 来获得您想要的任何功能,尽管这可能会为清除屏幕等简单的事情拖入相当多的东西。尽管如此,它仍然是一个值得认真考虑的选项,因为它为您提供了更多的灵活性。

回答by Naffi

#include <stdlib.h>
int main(void)
{
    system("clear");
}

回答by Basile Starynkevitch

It is usually not a matter of just clearing the screen, but of making a terminal aware application.

这通常不仅仅是清除屏幕的问题,而是制作终端感知应用程序的问题。

You should use the ncurseslibrary and read the NCURSES programming HowTo

您应该使用ncurses库并阅读NCURSES 编程 HowTo

(You could perhaps use some ANSI escape codesas David RFanswered, but I don't think it is a good idea)

(您也许可以使用一些ANSI 转义码作为大卫 RF 的回答,但我认为这不是一个好主意)

回答by Ryozuki

You can achieve this using CSI sequences:

您可以使用 CSI 序列实现此目的:

#include <stdio.h>
int main()
{
    printf("\x1b[H\x1b[J");
}

What does \x1b[H?

有什么作用\x1b[H

Actually it is the same as \x1b[1;1;H, it means that it will move the cursor to row 1 and column 1.

其实和 是一样的\x1b[1;1;H,表示将光标移动到第 1 行第 1 列。

What does \x1b[Ja.k.a \x1b[0;J?

什么\x1b[J又名\x1b[0;J

If n is 0 or missing, it will clear from cursor to end of screen.

如果 n 为 0 或缺失,它将从光标清除到屏幕末尾。

Source: https://en.wikipedia.org/wiki/ANSI_escape_code#CSI_sequences

来源:https: //en.wikipedia.org/wiki/ANSI_escape_code#CSI_sequences

回答by Andreadjk

This code is for clear screen with reset scrollbar position in terminal style windows#include <iostream> int main(){ std::cout << "\033c"; return 0;}

此代码用于清除屏幕并在终端样式窗口中重置滚动条位置#include <iostream> int main(){ std::cout << "\033c"; return 0;}

回答by Jonsmoke

To clear the screen using termcaps, use this :

要使用 termcaps 清除屏幕,请使用以下命令:

write(1, tgetstr("cl", 0), strlen(tgetstr("cl", 0)));

回答by Vishal Chincholi

Just use #include<stdlib.h>after #include<stdio.h>.

#include<stdlib.h>后使用即可#include<stdio.h>

Then you can use the command system("clear");after main() {

然后你可以在system("clear");之后使用命令main() {

i.e:

IE:

#include<stdio.h>
#include<stdlib.h>

int main()
{
    system("clear");

After these commands you can continue with your program.

在这些命令之后,您可以继续您的程序。

Hope this helps :)

希望这可以帮助 :)

回答by Ammar Saleem

Use system("clear");with header #include <stdlib.h>(for C Language) or #include <cstdlib>(for C++).

使用system("clear");与报头#include <stdlib.h>(对于C语言)或#include <cstdlib>(用于C ++)。