How to clean the console

As in the title. How to clear console in C ++?

+79
c ++ windows console-application dev-c ++
Jun 26 '11 at 19:45
source share
19 answers

For pure C ++

You can't C ++ doesn't even have a console concept.

The program can print to a printer, output directly to a file or redirect to the input of another program, despite all the worries. Even if you could clear the console in C ++, it would greatly complicate these cases.

See this entry in comp.lang. C ++ FAQ:

OS-specific

If it still makes sense to clear the console in your program and you are interested in specific solutions for the operating system, they exist.

For Windows (as in your tag) check this link:

  • How to clean the console in the assembly?

Change: This answer was previously mentioned using system("cls"); because Microsoft said to do this. However, in the comments it was noted that this is not safe to do . Due to this problem, I removed the link to a Microsoft article.

Libraries (somewhat portable)

ncurses is a library that supports console manipulation:

+62
Jun 26 '11 at 19:47
source share

For Windows through the console API:

 void clear() { COORD topLeft = { 0, 0 }; HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_SCREEN_BUFFER_INFO screen; DWORD written; GetConsoleScreenBufferInfo(console, &screen); FillConsoleOutputCharacterA( console, ' ', screen.dwSize.X * screen.dwSize.Y, topLeft, &written ); FillConsoleOutputAttribute( console, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE, screen.dwSize.X * screen.dwSize.Y, topLeft, &written ); SetConsoleCursorPosition(console, topLeft); } 

He happily ignores all possible errors, but, hey, this is console cleaning. It does not seem that system("cls") handles errors better.

For * nixes, you can usually use ANSI escape codes, so this would be:

 void clear() { // CSI[2J clears screen, CSI[H moves the cursor to top-left corner std::cout << "\x1B[2J\x1B[H"; } 

Using system for this is just ugly.

+47
Jun 26 '11 at 23:51
source share

For Linux / Unix and possibly for some others, but not for Windows up to 10 TH2:

 printf("\033c"); 

will reset the terminal.

+15
May 10 '17 at 6:05
source share

outputting multiple lines to the windows console is useless. It just adds blank lines to it. unfortunately, the path refers to specific windows and includes either conio.h (and clrscr () may not exist, this is not a standard header) or the Win API method

 #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 ); } 

For a POSIX system, this is simpler; you can use ncurses or terminal functions

 #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" ) ); } 
+7
Sep 29 '16 at 6:31
source share
 // #define _WIN32_WINNT 0x0500 // windows >= 2000 #include <windows.h> #include <iostream> using namespace std; void pos(short C, short R) { COORD xy ; xy.X = C ; xy.Y = R ; SetConsoleCursorPosition( GetStdHandle(STD_OUTPUT_HANDLE), xy); } void cls( ) { pos(0,0); for(int j=0;j<100;j++) cout << string(100, ' '); pos(0,0); } int main( void ) { // write somthing and wait for(int j=0;j<100;j++) cout << string(10, 'a'); cout << "\n\npress any key to cls... "; cin.get(); // clean the screen cls(); return 0; } 
+4
May 09 '15 at 7:37
source share

To clear the screen, you first need to enable the module:

 #include <stdlib.h> 

this will import the windows command. Then you can use the "system" function to run command commands (which edit the console). On Windows C ++, the screen cleaning command is:

 system("CLS"); 

And that will clear the console. All code will look like this:

 #include <iostream> #include <stdlib.h> using namespace std; int main() { system("CLS"); } 

And that’s all you need! Goodluck :)

+3
May 29 '14 at 7:35
source share

On Windows:

 #include <cstdlib> int main() { std::system("cls"); return 0; } 

On Linux / Unix:

 #include <cstdlib> int main() { std::system("clear"); return 0; } 
+3
Jul 05 '15 at 15:57
source share

This is REALLY mutable, but try:

 void cls() { for (int i = 0; i < 250; ++i) { std::cout << endl; } } 
+3
Mar 12 '16 at 20:02
source share

Use system("cls") to clear the screen:

 #include <stdlib.h> int main(void) { system("cls"); return 0; } 
+2
Jun 12 '16 at 7:38
source share

This is difficult to do when viewing the MAC, as it does not have access to Windows features that can help clear the screen. My best fix is ​​to loop and add lines until the terminal becomes clear, and then run the program. However, it is not so efficient or useful for memory if you use it first and often.

 void clearScreen(){ int clear = 5; do { cout << endl; clear -= 1; } while (clear !=0); } 
+2
May 6 '17 at 5:10
source share

The easiest way for me without having to reinvent the wheel.

 void Clear() { #if defined _WIN32 system("cls"); #elif defined (__LINUX__) || defined(__gnu_linux__) || defined(__linux__) system("clear"); #elif defined (__APPLE__) system("clear"); #endif } 
+2
Oct 19 '18 at 15:39
source share

Here is an easy way to do this:

 #include <iostream> using namespace std; int main() { cout.flush(); // Flush the output stream system("clear"); // Clear the console with the "system" function } 
0
May 14 '18 at 13:20
source share

On Windows, we have several options:

  1. clrscr () (header: conio.h)

  2. system ("cls") (header file: stdlib.h)

On Linux, use system (β€œclear”) (header file: stdlib.h)

0
Aug 21 '19 at 12:35
source share

Use System :: Console :: Clear ();

This will clear (empty) the buffer.

-one
Nov 26 '14 at 4:05
source share
 #include <cstdlib> void cls(){ #if defined(_WIN32) //if windows system("cls"); #else system("clear"); //if other #endif //finish 

}

Just calling cls () anywhere

-one
Apr 2 '17 at 9:47 on
source share

edit: completely redone question

Just check which system they are on and send a system command depending on the system. although this will be installed at compile time

 #ifdef __WIN32 system("cls"); #else system("clear"); // most other systems use this #endif 

This is a completely new method!

-2
Jun 12 '16 at 7:02
source share

Using the system (""), you can use the cleaning method of the operating system console;
for windows it will be a system ("cls"); eg
and instead of releasing three different codes for different operating systems. just create a way to get what os works for. You can do this by detecting if C # unique ifdef system variables exist
eg.

 enum OPERATINGSYSTEM = {windows = 0, mac = 1, linux = 2 /*etc you get the point*/}; void getOs(){ #ifdef _WIN32 return OPERATINGSYSTEM.windows #elif __APPLE__ //etc you get the point #endif } int main(){ int id = getOs(); if(id == OPERATINGSYSTEM.windows){ system("CLS"); }else if (id == OPERATINGSYSTEM.mac){ system("CLEAR"); } //etc you get the point } 
-2
Dec 19 '16 at 16:29
source share

use: clrscr ();

 #include <iostream> using namespace std; int main() { clrscr(); cout << "Hello World!" << endl; return 0; } 
-3
Apr 25 '15 at 20:13
source share

The easiest way is to flush the stream several times (ideally more than any possible console). 1024 * 1024 is probably a size that cannot be console.

 int main(int argc, char *argv) { for(int i = 0; i <1024*1024; i++) std::cout << ' ' << std::endl; return 0; } 

The only problem with this is the program cursor; this flashing thing (or non-flashing thing) depending on the platform / console will be at the end of the console, against it. However, this should never cause any problems.

-6
Jun 27 '11 at 3:52
source share



All Articles