Filling the console window

Hello,

I'm working in a console having 80 columns x 25 rows, which gives a total amount of 2000 positions that I intend to fill with a certain character. The source code seemed quite simple at the beginning but I came across an unexpected result: I get a blank row at the end because apparently I can't write the last character without jumping into the next line.

1
2
3
4
for ( int c = 0 ; c < 2000 ; c++)
{
    putchar('A'); 
}

Notice that in this case I'm trying to fill the screen with only one character, but in general I want the application to fill it with anything:
1
2
3
4
5
char alternate[] = {'A','B'}
for ( int c = 0 ; c < 2000 ; c++)
{
    putchar( alternate[c%2] ); 
}


Is there any way to achieve this?
Last edited on
The bad news is going to be that the console buffer size is a per instance user setting so there is no use in guessing at it with hard coded magic numbers. The good news is that you posted this in the right board so we know how to help you.

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
#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <wincon.h>

int main()
{
    HANDLE Con_Out = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_SCREEN_BUFFER_INFO ScreenBuf;
        GetConsoleScreenBufferInfo(Con_Out, &ScreenBuf);

    unsigned BufferSize = ScreenBuf.dwSize.X * ScreenBuf.dwSize.Y;

    COORD Start;
        Start.X = 0;
        Start.Y = 0;

    CONSOLE_CURSOR_INFO Cursor;
        Cursor.dwSize = 1;
        Cursor.bVisible = FALSE;
        SetConsoleCursorInfo(Con_Out, &Cursor);

    DWORD Garbage = 0;

    FillConsoleOutputCharacterA(Con_Out, 'A', BufferSize, Start, &Garbage);

    CloseHandle(Con_Out);

    return 0;
}


Each call to "FillConsoleOutputCharacterA()" will fill the screen with what ever 'char' you designate as the second parameter. Please don't tell me that this is for a game.
Thank you for your answer,

The purpose of this post is to have more control over the 'graphical' aspects of the console, for example the colour of the text and the background, the position of the cursor and in particular, being able to fill the console with whatever I want.
I was rather thinking of filling it with a string instead of just with one 'char'. In the second code I posted the result would be the screen filled with "AB".
This leads me to my next question: Is there any way to directly access the console screen buffer?
Is there any way to directly access the console screen buffer?

Yes, but the methods you would use vary depending on what aspect of it you want to work with. If you just want to write straight text to it then use the "WriteConsole()" function. If you want to change the color of the font then use "WriteConsoleOutput()" to change the setting for a single write operation or "SetConsoleTextAttribute()" to manage the setting globally. Changing the character set is done with "SetConsoleCP()" etc. The list goes on like this.
Thank you,

The function "WriteConsoleOutput()" is exactly what I was looking for as it allows me to control the whole screen buffer.
Topic archived. No new replies allowed.