除了使用 system("cls")
,C语言中是否有“正确”的清除控制台窗口的方法?
除了使用 system("cls")
,C语言中是否有“正确”的清除控制台窗口的方法?
printf("\e[1;1H\e[2J");
这个函数可以在ANSI终端上工作,需要POSIX。我假设也有一个版本可以在Windows控制台上工作,因为它也支持ANSI转义序列。
#include <unistd.h>
void clearScreen()
{
const char *CLEAR_SCREEN_ANSI = "\e[1;1H\e[2J";
write(STDOUT_FILENO, CLEAR_SCREEN_ANSI, 12);
}
还有一些其他选择,其中一些不会将光标移动到 {1,1}。
"\e[2J"
。我知道已经过去了四年,但是...能解释一下区别吗?或者 "\e[1;1H"
应该做什么? - Braden Best为了移植性,请尝试以下方法:
#ifdef _WIN32
#include <conio.h>
#else
#include <stdio.h>
#define clrscr() printf("\e[1;1H\e[2J")
#endif
然后只需调用clrscr()
。在Windows上,它将使用conio.h
的clrscr()
函数,在Linux上,它将使用ANSI转义码。
如果你真的想要“正确地”做到这一点,你可以消除中间人(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
一个在Windows(cmd.exe)、Linux(Bash和zsh)以及OS X(zsh)上测试过的解决方法:
#include <stdlib.h>
void clrscr()
{
system("@cls||clear");
}
system
calls commands for other languages (said, bash, zsh, batch, etc.). Still +1 to try to make it portable. (I've tested on debian/linux and win7, even inverting the arguments. No need the @ also, because the command will not be on the screen after run) → 楼主正在寻求C语言指令,而system
调用的是其他语言的指令(如said、bash、zsh、batch等)。尽管如此,还是要尝试使其具有可移植性。(我已在debian / linux和win7上进行了测试,甚至翻转了参数。也不需要@,因为命令将在运行后不会显示在屏幕上)。 - DrBeco使用宏可以检查当前操作系统是否为Windows、Linux、Mac或Unix,并根据当前平台调用相应的函数。示例代码如下:
void clear(){
#if defined(__linux__) || defined(__unix__) || defined(__APPLE__)
system("clear");
#endif
#if defined(_WIN32) || defined(_WIN64)
system("cls");
#endif
}
_WIN32
。 - jdt#include <conio.h>
并使用
clrscr()
由于你提到了cls
,这似乎是与Windows相关的。如果是这样,那么这篇KB文章中有可以实现它的代码。我刚试过了,当我使用以下代码调用它时,它起作用了:
cls( GetStdHandle( STD_OUTPUT_HANDLE ));
没有一种C语言的可移植方法来做到这一点。尽管各种光标操作库,如curses相对可移植。conio.h在OS/2 DOS和Windows之间可移植,但不能用于*nix变体。
"控制台"的整个概念是标准C范围之外的概念。
如果你正在寻找一个纯Win32 API解决方案,则Windows控制台API中没有单个调用可以完成此操作。一种方法是使用FillConsoleOutputCharacter足够大数量的字符。或者使用WriteConsoleOutput,你可以使用GetConsoleScreenBufferInfo找出需要多少个字符。
你还可以创建一个全新的控制台屏幕缓冲区并将其设置为当前缓冲区。
Windows:
system("cls");
在Windows系统中使用此命令可以清空控制台窗口。
Unix:
system("clear");
在Unix系统中使用此命令可以清空控制台窗口。
你也可以通过插入换行符来滚动屏幕,具体可以参考这里的内容。
通过这些方法,你可以轻松实现跨平台操作。
四十个 quadvigiseptanovatriheptasexgesillion
,我们在拥有一个带有强大的大数库的2048位CPU之前无法处理。抱歉,你没那么幸运。你只能接受这样一个事实,那些使用帝国大厦侧面作为投影监视器,并在全屏终端上运行1pt字体的人将获得一个丑陋的“清除”效果。这是一个罕见的边缘情况。否则,大约100行代码就可以解决问题。(在1080p显示器上以默认字体运行的Xterm仅有74行) - Braden Best只需在 void main() 中键入 clrscr(); 函数。
例如:
void main()
{
clrscr();
printf("Hello m fresher in programming c.");
getch();
}
clrscr();
实现清屏的简单函数。