HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Clearing the screen using FillConsoleOutputAttribute()

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
thefillconsoleoutputattributeclearingusingscreen

Problem

I'm currently clearing my console window with this piece of code:

void clrScr()
{
    COORD cMap =
    {
        0, 3
    };
    if(!FillConsoleOutputAttribute(hCon, 0, 2030, cMap, &count))
    {
        std::cout << "Error clearing the console screen." << std::endl;
        std::cout << "Error code: " << GetLastError() << std::endl;
        std::cin.get();
    }
}


, which I call once in the main loop.

But since my window is quite large (70x35), it's flickering quite a bit.

I was wondering if there are any faster methods of doing this?

Solution

Here is an idea; ANSI escape codes work on Windows, Linux and OSX:

cout << "\033c";


\033 - stands for octal ESC,
c - resets the device (terminal is default) to the initial state ( clear the screen,clear the buffer also so it is not possible to scroll, reset the fonts and so on).

In some cases it may be more useful than well known which simply adds some empty lines and puts the cursor in the upper left corner:

cout << "\033[2J\033[1;1H"

Code Snippets

cout << "\033c";
cout << "\033[2J\033[1;1H"

Context

StackExchange Code Review Q#19274, answer score: 4

Revisions (0)

No revisions yet.