Gibt es eine “richtige” Möglichkeit, das Konsolenfenster in C zu löschen, außer mit system("cls")
?
Wie löschen Sie den Konsolenbildschirm in C?
verschlingt
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.
#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}.
-
Just to let you know, FWIW, this sequence as is didn’t work for me in a windows cmd.exe console.
– dodgy_coderApr 9, 2013 at 7:56
-
@anon : This if for UNIX. Do you do the answer for DOS?
– user2284570Mar 22, 2015 at 21:41
-
How does it demand POSIX? I don’t believe those escape sequences are specified by the POSIX standard.
– Keith ThompsonMay 31, 2015 at 21:53
-
That doesn’t work for me. What does work for me, though, is plain old
"\e[2J"
. I know it’s been like four years, but… Care to explain the difference? Or what the"\e[1;1H"
is supposed to do?– Braden BestSep 18, 2015 at 23:31
-
This code seems to work fine on Windows’ CMD, at least in Win10.
– NicMay 13, 2016 at 1:05
Tom
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?
Portability is an issue, no matter what library is used.
-
I +1’d you before reading your line about conio.h. Note that, too, is highly non-portable.
– Derrick TurkFeb 27, 2010 at 17:05
-
I’m not sure about conio.h, but it looks like curses takes care of the GUI in a more comprehensive way than I was initially imagining. I’ll have to look into this. Thanks for the suggestion!
– devursMar 1, 2010 at 2:12
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.
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):
#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
-
Actually this is really nice. +1 for pointing the “portable” way using ANSI escapes with the horrible but useful clrscr() from conio.h.
– Manoel VilelaAug 29, 2017 at 22:55
-
Just for saying: clrscr() is not available on conio.h implementation of mingw compiler
– Manoel VilelaSep 21, 2017 at 12:48
A workaround tested on Windows(cmd.exe), Linux(Bash and zsh) and OS X(zsh):
#include <stdlib.h>
void clrscr()
{
system("@cls||clear");
}
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:
void clear(){
#if defined(__linux__) || defined(__unix__) || defined(__APPLE__)
system("clear");
#endif
#if defined(_WIN32) || defined(_WIN64)
system("cls");
#endif
}
-
_WIN32
will be defined on Windows even if you build for 64-bit.– jdtOct 9, 2021 at 14:41
Mark Wilkins
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( GetStdHandle( STD_OUTPUT_HANDLE ));
-
_WIN32
will be defined on Windows even if you build for 64-bit.– jdtOct 9, 2021 at 14:41
Zombo
#include <conio.h>
and use
clrscr()
-
Do note that this is not portable.
– Billy ONealFeb 27, 2010 at 17:40
-
And it is not in c standard. Note that , OP mentioned Is there a “proper” way
– Muthu Ganapathy NathanAug 30, 2011 at 16:19
cplusplus.com/forum/articles/10515 enthält einige Codes. Obwohl es nicht sowohl für Windows- als auch für POSIX-Systeme portierbar ist, kann es für jeden nützlich sein, der diese Frage in Zukunft liest.
– Aseem Bansal
22. Juni 2013 um 16:02 Uhr
Auch hier cplusplus.com/articles/4z18T05o
– Marcello Romani
27. Juni 2013 um 6:36 Uhr