简体   繁体   中英

Console showing end of string character

I'm just messing around with CPP and Windows console. I need to change a specific part of the screen buffer I have but it always prints the string I want plus an empty square box.

Is there a way to remove this blank box?

int screen_w = 120;
int screen_h = 30;

void init_screen(wchar_t* screen_buffer, int screen_w, int screen_h) {
    for (int i = 0; i < screen_h; ++i) {
        for (int j = 0; j < screen_w; ++j) {
            if (i != 0 && i != 2 && i != screen_h - 1 && 
                j != 0 && j != screen_w - 1) {
                screen_buffer[i * screen_w + j] = ' ';
            } else {
                screen_buffer[i * screen_w + j] = '#';
            }
        }
    }
}

int main() {
    int screen_s = screen_w * screen_h;
    
    wchar_t *screen_buffer = 0;
    screen_buffer = (wchar_t *) calloc(screen_s, sizeof *screen_buffer);
    
    HANDLE c_handle = CreateConsoleScreenBuffer(
        GENERIC_WRITE | GENERIC_READ,
        0,
        0,
        CONSOLE_TEXTMODE_BUFFER,
        0);
    SetConsoleActiveScreenBuffer(c_handle);
    DWORD bytes_written = 0;
    
    init_screen(screen_buffer, screen_w, screen_h);

    while (1) {
        Sleep(1000 / 10);
        WriteConsoleOutputCharacterW(c_handle, screen_buffer, screen_s, { 0, 0 }, &bytes_written);
        wsprintfW(&screen_buffer[15 * screen_w + 60], L"hello world"); // THIS IS WHAT I DO. 
                                                                       // TRIED OTHER SIMILAR FUNCTIONS.
        if (GetAsyncKeyState(VK_SPACE) & 0x8000) break;
    }
    free(screen_buffer = 0);
    return 0;
}

And what it shows:

########################################################################################################################
#                                                                                                                      #
########################################################################################################################
#                                                                                                                      #
#                                                                                                                      #
#                                                                                                                      #
#                                                                                                                      #
#                                                                                                                      #
#                                                                                                                      #
#                                                                                                                      #
#                                                                                                                      #
#                                                                                                                      #
#                                                                                                                      #
#                                                                                                                      #
#                                                                                                                      #
#                                                           hello world▯                                               #
#                                                                                                                      #
#                                                                                                                      #
#                                                                                                                      #
#                                                                                                                      #
#                                                                                                                      #
#                                                                                                                      #
#                                                                                                                      #
#                                                                                                                      #
#                                                                                                                      #
#                                                                                                                      #
#                                                                                                                      #
#                                                                                                                      #
#                                                                                                                      #
########################################################################################################################```

The problem is that your wsprintfW() call (or any of the sprintf function family) will append the (required) nul -terminator character to the output buffer segment ... and this may be rendered as a 'strange' character in some platforms' consoles.

So, rather than trying to write a formatted string to the buffer, just use a direct memory-copy routine, instead.

The following line - in place of your wsprintf(...) call - will do the trick:

    memcpy(&screen_buffer[15 * screen_w + 60], L"hello world", wcslen(L"hello world") * sizeof(wchar_t));

but you could make the code look prettier/simpler using a declared (local) variable:

    const wchar_t* text = L"hello world";
    memcpy(&screen_buffer[15 * screen_w + 60], text, wcslen(text) * sizeof(wchar_t));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM