控制台显示字符串结尾

时间:2020-09-27 21:28:09

标签: c++ windows-console

我只是在搞混CPP和Windows控制台。我需要更改屏幕缓冲区的特定部分,但它始终会打印我想要的字符串以及一个空的方形框。

是否可以删除此空白框?

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;
}

显示的内容:

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

1 个答案:

答案 0 :(得分:3)

问题是您的wsprintfW()调用(或任何sprintf函数系列)会将(必需)nul终止符附加到输出缓冲区段中...并且在某些平台的控制台中,此 可能被呈现为“奇怪”字符。

因此,与其尝试将 formatted 字符串写入缓冲区,不如直接使用直接的内存复制例程。

以下行-代替您的wsprintf(...)呼叫-将达到目的:

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

但是您可以使用声明的(本地)变量使代码看起来更漂亮/更简单:

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