如何从 stdout 控制台光标指向的位置读取当前字符

how to read current character from what stdout console cursor pointing on

我正在编写代码,其中只有控制台的光标在移动,不一定是因为键盘输入。 我需要一个简单的 way/function 来从 stdout 的光标当前指向的位置读取当前字符。 有什么建议吗?

(Windows 10,win32 应用程序通过 VS 2017)

据我所知,没有可移植的方法。

在 Windows、ReadConsoleOutputCharacter. To find out cursor position, call GetConsoleScreenBufferInfo

在 Linux、mvinch from <curses.h> will read characters. To find out where to read, getyx

在其他平台上情况有所不同。

终于成功了(Windows)。 谁也需要这个的答案:

char cursorCharRead()
{
    char buf[BUFSIZ];
    CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
    HANDLE hConsole= GetStdHandle(STD_OUTPUT_HANDLE);
    GetConsoleScreenBufferInfo(hConsole, &csbiInfo);
    COORD pos = csbiInfo.dwCursorPosition; //set pos to current cursor location
    TCHAR strFromConsole[1];    //need space to only one char
    DWORD dwChars;
    ReadConsoleOutputCharacter(
    hConsole,
    strFromConsole, // Buffer where store symbols
    1, // Read 1 char to strFormConsole
    pos, // Read from current cursor position
    &dwChars); // How many symbols stored
char c = strFromConsole[0];
return c;

}

此函数将return控制台光标当前指向的字符