Is there a "proper" way to clear the console window in C, besides using system("cls")
?
除了使用system(“cls”)之外,是否有“正确”的方法清除C中的控制台窗口?
12 个解决方案
#1
#2
20
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.
POSIX要求该函数在ANSI终端上工作。我假设有一个版本可能在窗口的控制台工作,因为它也支持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}。
#3
10
Since you mention cls
, it sounds like you are referring to windows. If so, then this KB item has the code that will do it. I just tried it, and it worked when I called it with the following code:
既然你提到了cls,听起来你指的是windows。如果是这样,那么这个KB项具有执行此操作的代码。我试过了,当我用下面的代码调用它时,它奏效了:
cls( GetStdHandle( STD_OUTPUT_HANDLE ));
#4
8
#include <conio.h>
and use
和使用
clrscr()
#5
8
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");
}
#6
5
There is no C portable way to do this. Although various cursor manipulation libraries like curses are relatively portable. conio.h is 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 WriteConsoleOutput You can use GetConsoleScreenBufferInfo to find out how many characters will be enough.
如果您正在寻找一个纯Win32 API解决方案,那么在Windows控制台API中没有一个调用可以做到这一点。一种方法是输入足够多的字符。或者是WriteConsoleOutput,你可以使用GetConsoleScreenBufferInfo来计算多少字符就足够了。
You can also create an entirely new Console Screen Buffer and make the the current one.
您还可以创建一个全新的控制台屏幕缓冲区,并创建当前的缓冲区。
#7
4
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
}
#8
4
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的clrscr(),在Linux上,它将使用ANSI转义代码。
If you really want 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
#9
3
Windows:
窗口:
system("cls");
系统(cls);
Unix:
Unix:
system("clear");
系统(“明确”);
You could instead, insert newline chars until everything gets scrolled, take a look here.
你可以插入换行字符直到所有内容都被卷起来,看看这里。
With that, you achieve portability easily.
这样,您就可以轻松地实现可移植性。
#10
1
just type clrscr(); function in void main().
只要输入clrscr();函数void main()。
as example:
为例:
void main()
{
clrscr();
printf("Hello m fresher in programming c.");
getch();
}
clrscr();
function easy to clear screen.
功能简单,屏幕清晰。
#11
0
The proper way to do it is by using tput
or terminfo
functions to obtain terminal properties and then insert newlines according to the dimensions..
正确的方法是使用tput或terminfo函数来获取终端属性,然后根据尺寸插入新行。
#12
-3
This should work. Then just call cls(); whenever you want to clear the screen.
这应该工作。然后就叫cls();无论何时你想清除屏幕。
(using the method suggested before.)
(使用之前建议的方法)
#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");
}
}
#1
22
Well, C doesn't understand the concept of screen. So any code would fail to be portable. Maybe take a look at conio.h or curses, according to your needs?
C不理解屏幕的概念。因此,任何代码都无法移植。也许看看康诺吧。h或curses,根据你的需要?
Portability is an issue, no matter what library is used.
不管使用什么库,可移植性都是一个问题。
#2
20
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.
POSIX要求该函数在ANSI终端上工作。我假设有一个版本可能在窗口的控制台工作,因为它也支持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}。
#3
10
Since you mention cls
, it sounds like you are referring to windows. If so, then this KB item has the code that will do it. I just tried it, and it worked when I called it with the following code:
既然你提到了cls,听起来你指的是windows。如果是这样,那么这个KB项具有执行此操作的代码。我试过了,当我用下面的代码调用它时,它奏效了:
cls( GetStdHandle( STD_OUTPUT_HANDLE ));
#4
8
#include <conio.h>
and use
和使用
clrscr()
#5
8
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");
}
#6
5
There is no C portable way to do this. Although various cursor manipulation libraries like curses are relatively portable. conio.h is 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 WriteConsoleOutput You can use GetConsoleScreenBufferInfo to find out how many characters will be enough.
如果您正在寻找一个纯Win32 API解决方案,那么在Windows控制台API中没有一个调用可以做到这一点。一种方法是输入足够多的字符。或者是WriteConsoleOutput,你可以使用GetConsoleScreenBufferInfo来计算多少字符就足够了。
You can also create an entirely new Console Screen Buffer and make the the current one.
您还可以创建一个全新的控制台屏幕缓冲区,并创建当前的缓冲区。
#7
4
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
}
#8
4
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的clrscr(),在Linux上,它将使用ANSI转义代码。
If you really want 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
#9
3
Windows:
窗口:
system("cls");
系统(cls);
Unix:
Unix:
system("clear");
系统(“明确”);
You could instead, insert newline chars until everything gets scrolled, take a look here.
你可以插入换行字符直到所有内容都被卷起来,看看这里。
With that, you achieve portability easily.
这样,您就可以轻松地实现可移植性。
#10
1
just type clrscr(); function in void main().
只要输入clrscr();函数void main()。
as example:
为例:
void main()
{
clrscr();
printf("Hello m fresher in programming c.");
getch();
}
clrscr();
function easy to clear screen.
功能简单,屏幕清晰。
#11
0
The proper way to do it is by using tput
or terminfo
functions to obtain terminal properties and then insert newlines according to the dimensions..
正确的方法是使用tput或terminfo函数来获取终端属性,然后根据尺寸插入新行。
#12
-3
This should work. Then just call cls(); whenever you want to clear the screen.
这应该工作。然后就叫cls();无论何时你想清除屏幕。
(using the method suggested before.)
(使用之前建议的方法)
#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");
}
}