C语言 你如何清除 C 中的控制台屏幕?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2347770/
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
How do you clear the console screen in C?
提问by devurs
Is there a "proper" way to clear the console window in C, besides using system("cls")?
除了使用system("cls")?
采纳答案by Tom
回答by Avinash Katiyar
printf("\e[1;1H\e[2J");
This function will work on ANSI terminals, demands POSIX. I assume there is a version that might also work on window's console, since it also supports ANSI escape sequences.
此功能将在 ANSI 终端上工作,需要 POSIX。我假设有一个版本也可以在窗口的控制台上运行,因为它也支持 ANSI 转义序列。
#include <unistd.h>
void clearScreen()
{
const char *CLEAR_SCREEN_ANSI = "\e[1;1H\e[2J";
write(STDOUT_FILENO, CLEAR_SCREEN_ANSI, 12);
}
There are some other alternatives, some of which don't move the cursor to {1,1}.
还有一些其他 选择,其中一些不会将光标移动到 {1,1}。
回答by MD XF
For portability, try this:
为了便携性,试试这个:
#ifdef _WIN32
#include <conio.h>
#else
#include <stdio.h>
#define clrscr() printf("\e[1;1H\e[2J")
#endif
Then simply call clrscr(). On Windows, it will use conio.h's clrscr(), and on Linux, it will use ANSI escape codes.
然后只需调用clrscr(). 在 Windows 上,它将使用conio.h's clrscr(),而在 Linux 上,它将使用 ANSI 转义码。
If you reallywant to do it "properly", you can eliminate the middlemen (conio, printf, etc.) and do it with just the low-level system tools (prepare for a massive code-dump):
如果你真的想“正确地”做到这一点,你可以消除中间人(conio,printf等),只使用低级系统工具(为大量代码转储做准备):
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
void ClearScreen()
{
HANDLE hStdOut;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD count;
DWORD cellCount;
COORD homeCoords = { 0, 0 };
hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
if (hStdOut == INVALID_HANDLE_VALUE) return;
/* Get the number of cells in the current buffer */
if (!GetConsoleScreenBufferInfo( hStdOut, &csbi )) return;
cellCount = csbi.dwSize.X *csbi.dwSize.Y;
/* Fill the entire buffer with spaces */
if (!FillConsoleOutputCharacter(
hStdOut,
(TCHAR) ' ',
cellCount,
homeCoords,
&count
)) return;
/* Fill the entire buffer with the current colors and attributes */
if (!FillConsoleOutputAttribute(
hStdOut,
csbi.wAttributes,
cellCount,
homeCoords,
&count
)) return;
/* Move the cursor home */
SetConsoleCursorPosition( hStdOut, homeCoords );
}
#else // !_WIN32
#include <unistd.h>
#include <term.h>
void ClearScreen()
{
if (!cur_term)
{
int result;
setupterm( NULL, STDOUT_FILENO, &result );
if (result <= 0) return;
}
putp( tigetstr( "clear" ) );
}
#endif
回答by Jamesits
A workaround tested on Windows(cmd.exe), Linux(Bash and zsh) and OS X(zsh):
在 Windows(cmd.exe)、Linux(Bash 和 zsh) 和 OS X(zsh) 上测试的解决方法:
#include <stdlib.h>
void clrscr()
{
system("@cls||clear");
}
回答by nbro
Using macros you can check if you're on Windows, Linux, Mac or Unix, and call the respective function depending on the current platform. Something as follows:
使用宏,您可以检查您使用的是 Windows、Linux、Mac 还是 Unix,并根据当前平台调用相应的函数。如下:
void clear(){
#if defined(__linux__) || defined(__unix__) || defined(__APPLE__)
system("clear");
#endif
#if defined(_WIN32) || defined(_WIN64)
system("cls");
#endif
}
回答by Mark Wilkins
Since you mention cls, it sounds like you are referring to windows. If so, then this KB itemhas the code that will do it. I just tried it, and it worked when I called it with the following code:
既然你提到了cls,听起来你指的是 windows。如果是这样,则此知识库项目具有执行此操作的代码。我刚刚尝试过,当我使用以下代码调用它时它起作用了:
cls( GetStdHandle( STD_OUTPUT_HANDLE ));
回答by Vivek Sharma
#include <conio.h>
and use
并使用
clrscr()
回答by John Knoeller
There is no C portable way to do this. Although various cursor manipulation libraries like curses are relatively portable. conio.his portable between OS/2 DOS and Windows, but not to *nix variants.
没有 C 可移植的方法来做到这一点。尽管诸如curses 之类的各种光标操作库相对可移植。 conio.h可在 OS/2 DOS 和 Windows 之间移植,但不能移植到 *nix 变体。
The entire notion of a "console" is a concept outside of the scope of standard C.
“控制台”的整个概念是一个超出标准 C 范围的概念。
If you are looking for a pure Win32 API solution, There is no single call in the Windows console API to do this. One way is to FillConsoleOutputCharacter of a sufficiently large number of characters. Or WriteConsoleOutputYou can use GetConsoleScreenBufferInfo to find out how many characters will be enough.
如果您正在寻找纯 Win32 API 解决方案,Windows 控制台 API 中没有单独的调用来执行此操作。一种方法是填充足够多的字符的FillConsoleOutputCharacter。或者WriteConsoleOutput您可以使用GetConsoleScreenBufferInfo来找出多少个字符就足够了。
You can also create an entirely new Console Screen Buffer and make the the current one.
您还可以创建一个全新的控制台屏幕缓冲区并使其成为当前的。
回答by gfe
回答by Hyman
The proper way to do it is by using tputor terminfofunctions to obtain terminal properties and then insert newlines according to the dimensions..
正确的方法是使用tput或terminfo函数来获取终端属性,然后根据尺寸插入换行符。

