How to Access the Screen Buffer in C++

Written by alexander gokhfeld
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Access the Screen Buffer in C++
C++ language allows users to read data from the console screen buffer. ( Images)

A console is a window where users interact with an operating system by entering text using the computer keyboard and reading text output from the computer screen. An application can read data from the console input. The text that you read from a console is stored in the console screen buffer. The screen buffer is a grid of rows and columns, where each grid cell contains a character. By providing access to individual characters or entire lines from the console screen buffer, you can use input data in your C++ program.

Skill level:


  1. 1

    Click "Start" in Windows, then "Microsoft Visual Studio."

  2. 2

    Click "File," select "Open" and double-click on the C++ program file, in which you want to access a screen buffer of the system console.

  3. 3

    Enter the following code in the open window:

    define PERR(bSuccess, api)

    {if(!(bSuccess)) printf("%s:Error %d from %s \ on line %d\n",

    FILE, GetLastError(), api, LINE);}

    void cls( HANDLE hConsole )


    COORD coordScreen = { 0, 0 };    /* here's where we'll home

    the cursor */

    BOOL bSuccess;
    DWORD cCharsWritten;
    CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */
    DWORD dwConSize;                 /* number of character cells

    in the current buffer */

    /* get the number of character cells in the current buffer */
    bSuccess = GetConsoleScreenBufferInfo( hConsole, &csbi );
    PERR( bSuccess, "GetConsoleScreenBufferInfo" );
    dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
    /* set the buffer's attributes accordingly */
    bSuccess = FillConsoleOutputAttribute(hConsole,


       dwConSize, coordScreen, &cCharsWritten );
    PERR( bSuccess, "FillConsoleOutputAttribute" );
    /* put the cursor at (0, 0) */
    bSuccess = SetConsoleCursorPosition( hConsole, coordScreen );
    PERR( bSuccess, "SetConsoleCursorPosition" );
  4. 4

    Click "File" and "Save" to add the function of reading from the system console screen buffer to your C++ program.

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.