如何从stdout控制台光标指向的地方读取当前字符 [英] how to read current character from what stdout console cursor pointing on

查看:38
本文介绍了如何从stdout控制台光标指向的地方读取当前字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写代码,其中只有控制台的光标在移动,而不一定是因为键盘输入.我需要一种简单的方法/函数来从 stdout 的光标当前指向的内容中读取当前字符.有什么建议吗?

I'm writing code where only the cursor of the console is moving and not necessarily becauase of keyboard input. i need a simple way/function to read current char from what the cursor of stdout is currently pointing on. any suggestions?

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

(Windows 10,win32 application via VS 2017)

推荐答案

终于成功(Windows).任何人也需要这个的答案:

finally succeeded(Windows). the answer to whoever also needed this:

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;

}

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

this function will return the char the cursor of the console is currently pointing on

这篇关于如何从stdout控制台光标指向的地方读取当前字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆