简体   繁体   English

如何使用 \x1b[2j 清除屏幕?

[英]How to clear the screen with \x1b[2j?

How do we implement clrscr() ?我们如何实施clrscr() Googling it I found that \x1b[2j can be used to clear the screen but how do we use it?谷歌搜索我发现\x1b[2j可以用来清除屏幕,但我们如何使用它呢?

The standard C library doesn't provide a way of clearing the screen.标准 C 库不提供清除屏幕的方法。 You need an operating-system-dependent library for that.为此,您需要一个依赖于操作系统的库。

Under DOS and Windows, for a program running in a DOS or Windows console, you can use the DOS/Windows extensions provided in the core C library shipped with the OS:在 DOS 和 Windows 下,对于在 DOS 或 Windows 控制台中运行的程序,您可以使用操作系统附带的核心 C 库中提供的 DOS/Windows 扩展:

#include <conio.h>
clrscr();

Under unix systems, you can use the curses library, which is provided with the OS.在 unix 系统下,您可以使用操作系统提供的 curses 库。 Ports of the curses library exist for most operating systems, including Windows, so this is the way to go in a portable program.大多数操作系统(包括 Windows)都存在 Curses 库的端口,因此这是可移植程序的方法。 Link your program with -lcurses and use使用-lcurses链接您的程序并使用

#include <curses.h>
erase();

Some terminals and terminal emulators perform special functions such as clearing the screen when they receive an escape sequence.某些终端和终端模拟器执行特殊功能,例如在收到转义序列时清除屏幕。 Most terminals follow the ANSI standard which defines a number of escape sequences;大多数终端都遵循 ANSI 标准,该标准定义了许多转义序列; "\\x1b[2J" is such a sequence, and its effect is to clear the screen. "\\x1b[2J"就是这样一个序列,它的作用就是清屏。 Note the capital J .注意大写J On such a terminal, fputs("\\x1b[2J", stdout) clears the screen.在这样的终端上, fputs("\\x1b[2J", stdout)清除屏幕。 This is in fact what the curses library does when you call erase() on such a terminal;这实际上就是在这样的终端上调用erase()时 curses 库所做的; the curses library includes a database of terminal types and what escape sequences to use on the various types. curses 库包括一个终端类型数据库以及用于各种类型的转义序列。

If you're confident that is the control sequence you need to use, then:如果您确信这是您需要使用的控制序列,那么:

#include <stdio.h>

int main(void)
{
    fputs("\x1b[2j", stdout);
    return(0);
}

This deliberately omits the newline - but you might be better off with adding one after the 'j'.这故意省略了换行符 - 但在 'j' 后添加一个可能会更好。 However, as Gilles points out in his answer , there are other ways to do it which have merits compared with this solution.但是,正如Gilles在他的回答中指出的那样,与此解决方案相比,还有其他方法可以做到这一点。

On Windows you may try在 Windows 上,您可以尝试

#include <tchar.h>
#include <stdio.h>
#include <windows.h>

void clrscr(void)
{
 HANDLE std_out = GetStdHandle(STD_OUTPUT_HANDLE);
 CONSOLE_SCREEN_BUFFER_INFO cbi;
 COORD origin = {0,0};
 int buf_length;

 GetConsoleScreenBufferInfo(std_out,&cbi);
 buf_length = cbi.dwSize.X*cbi.dwSize.Y;
 FillConsoleOutputCharacter(std_out,0x20,buf_length,origin,0);
 FillConsoleOutputAttribute(std_out,0x07,buf_length,origin,0);
}

int _tmain(int argc, wchar_t *argv[], wchar_t *envp[])
{
 DWORD i;
 _tprintf(TEXT("Clear screen probe...\n"));
 clrscr();

 return 0;
}

"\\x1b[H\\x1b[2J" works on OSX. "\\x1b[H\\x1b[2J" 在 OSX 上工作。

This is a so called ANSI Escape Sequence that controls text terminal and will work with any ANSI compatible Terminal [1]:这是一个所谓的 ANSI 转义序列,它控制文本终端,并将与任何兼容 ANSI 的终端 [1] 一起使用:

  • \\x1b[2J clear screen and send cursor to home position. \\x1b[2J清除屏幕并将光标发送到\\x1b[2J位置。
  • \\x1b[H send cursor to cursor home position. \\x1b[H将光标发送到光标\\x1b[H位置。

Just printf("\\x1B[2J");只是printf("\\x1B[2J"); or echo "\\x1B[2J" it or whatever print method suits you :-) I use that with printf() for initial terminal clear screen when producing a debug output logs from an embedded system over uart connection, but you can use it as well on a standard Unix-like terminal with echo :-)echo "\\x1B[2J"它或任何适合您的打印方法:-) 当通过 uart 连接从嵌入式系统生成调试输出日志时,我将它与printf()用于初始终端清除屏幕,但您可以将其用作在带有echo的标准类 Unix 终端上运行良好 :-)

[1] https://en.wikipedia.org/wiki/ANSI_escape_code [1] https://en.wikipedia.org/wiki/ANSI_escape_code

#include <unistd.h>    
write(STDOUT_FILENO, "\x1b[2J", 4);

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

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