Intro

When writing some console app, we sometimes have requirements to interactive with the console screen directly. One example is clearing the console screen.

In this article, you will learn how to clear the console screen properly in C++ code.

Bad Implement

To clear the console screen, one of the approach is directly using the system console command:

1
2
3
4
5
6
7
8
9
10
11
12
#include <cstdlib> // required for `system`

void clearScreen() {
// On Windows:
system("cls");

// ----OR----

// On LINUX/UN*X:
system("clear");
}

Yeah… It works. However, there are some problems.

  1. Running an external application cost a lot of system resources. If you need to clear the console screen frequently, maybe one time per second or even higher, you’d better try other ways.
  2. This approach depends on the OS. If you are writing some cross-platform program, you have to use some macro tricks to workaround it. And if you are writing a program for some special Linux platform, the clear command could just unavailable, which can break your program.

A universal approach to clear the screen (Recommend)

Fortunately, almost all terminals now follow a standard. Which allow us to have a universal way to clear the console.

Try:

1
2
3
4
5

void clearScreen() {
std::cout << "\033[2J\033[1;1H";
}

In this approach, the first escape code \033[2J can clear the screen, and the second escape code \033[1;1H can move the cursor to (1,1).

Better approach on Windows platform

This approach is only available on Windows! If you have the requirement to target to Linux/UN*X system, just use the previous code directly.

Although the previous example work well on Windows, there is one obvious shortcoming: after clearing the screen, you can still see the scrollbar exist, and if you scroll up, you can see the old log is still there.

Unfortunately, to resolve this problem, we have to use a native approach.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

#include <Windows.h>

void clearScreen()
{
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coordScreen = { 0, 0 }; // home for the cursor
DWORD cCharsWritten;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD dwConSize;


// Get the number of character cells in the current buffer.
if( !GetConsoleScreenBufferInfo( hConsole, &csbi ))
{
return;
}

dwConSize = csbi.dwSize.X * csbi.dwSize.Y;

// Fill the entire screen with blanks.

if( !FillConsoleOutputCharacter( hConsole, // Handle to console screen buffer
(TCHAR) ' ', // Character to write to the buffer
dwConSize, // Number of cells to write
coordScreen, // Coordinates of first cell
&cCharsWritten ))// Receive number of characters written
{
return;
}

// Get the current text attribute.

if( !GetConsoleScreenBufferInfo( hConsole, &csbi ))
{
return;
}

// Set the buffer's attributes accordingly.

if( !FillConsoleOutputAttribute( hConsole, // Handle to console screen buffer
csbi.wAttributes, // Character attributes to use
dwConSize, // Number of cells to set attribute
coordScreen, // Coordinates of first cell
&cCharsWritten )) // Receive number of characters written
{
return;
}

// Put the cursor at its home coordinates.

SetConsoleCursorPosition( hConsole, coordScreen );
}

Related & Reference

  1. Microsoft docs: https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences
  2. https://stackoverflow.com/questions/17335816/clear-screen-using-c
  3. https://stackoverflow.com/questions/9597844/clear-screen-command-in-c