如何清除C中的控制台屏幕?

除了使用system("cls")之外,是否有清除C中控制台窗口的“正确”方法?

那么,C不理解屏幕的概念。 所以任何代码都不能移植。 也许看看conio.h或者curses ,根据你的需求?

可移植性是一个问题,无论使用什么库。

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

此function将在ANSIterminal上工作,要求POSIX。 我假设有一个版本也可以在窗口的控制台上工作,因为它也支持ANSI转义序列。

 #include <unistd.h> void clearScreen() { const char *CLEAR_SCREEN_ANSI = "\e[1;1H\e[2J"; write(STDOUT_FILENO, CLEAR_SCREEN_ANSI, 12); } 

还有一些其他的select ,其中一些不移动光标到{1,1}。

既然你提到了cls ,那听起来你就是指windows。 如果是这样,那么这个知识库项目有代码将做到这一点。 我只是试了一下,当我用下面的代码调用它时,它工作:

 cls( GetStdHandle( STD_OUTPUT_HANDLE )); 
 #include <conio.h> 

并使用

 clrscr() 

在Windows(cmd.exe),Linux(Bash和zsh)和OS X(zsh)上testing的解决方法:

 #include <stdlib.h> void clrscr() { system("@cls||clear"); } 

没有C便携式的方式来做到这一点。 尽pipe像curses这样的各种游标操作库是相对便携的。 conio.h可以在OS / 2 DOS和Windows之间移植,但不适用于* nix变体。

“控制台”的整个概念是标准C范围之外的一个概念。

如果您正在寻找一个纯粹的Win32 API解决scheme,Windows控制台API中没有任何一个调用来执行此操作。 一种方法是填充足够多的字符的FillConsoleOutputCharacter 。 或WriteConsoleOutput您可以使用GetConsoleScreenBufferInfo来找出有多less个字符就足够了。

您也可以创build一个全新的控制台屏幕缓冲区并使其成为当前的屏幕缓冲区。

使用macros可以检查你是否在Windows,Linux,Mac或Unix上,并根据当前平台调用相应的函数。 如下所示:

 void clear(){ #if defined(__linux__) || defined(__unix__) || defined(__APPLE__) system("clear"); #endif #if defined(_WIN32) || defined(_WIN64) system("cls"); #endif } 

为便于携带,请尝试以下操作:

 #ifdef _WIN32 #include <conio.h> #else #include <stdio.h> #define clrscr() printf("\e[1;1H\e[2J") #endif 

然后只需调用clrscr() 。 在Windows上,它将使用conio.hclrscr() ,在Linux上,它将使用ANSI转义码。

如果你真的想要“正确地”这样做,你可以省略中间商( conioprintf等),只用底层的系统工具(准备大量的代码转储):

 #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 

视窗:

system("cls");

Unix的:

system("clear");

你可以改为插入换行符,直到所有东西都滚动,看看这里 。

这样,您可以轻松实现便携性。

只需键入clrscr(); 函数在void main()中。

例如:

 void main() { clrscr(); printf("Hello m fresher in programming c."); getch(); } clrscr(); 

function易于清除屏幕。

正确的方法是使用tput或者terminfo函数来获取terminal属性,然后根据维度插入换行符。

这应该工作。 然后调用cls(); 每当你想清除屏幕。

(使用之前build议的方法)。

 #include <stdio.h> void cls() { int x; for ( x = 0; x < 10; x++ ) { printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); } }