从stdin读取非阻塞

时间:2013-02-04 04:54:07

标签: c windows console nonblocking

我希望在读取特定字节数后返回ReadConsoleW()。 但它并没有回归。

如果在完成读取指定的字节数后,如何使ReadConsoleW()返回?

我试过的代码在这里:

#include <stdio.h>
#include <Windows.h>


int main()
{
    //something is being written to stdin.
    Sleep(2000);
    int b;
    int r;
    //read 3 wide character
    ReadConsoleW(GetStdHandle(STD_INPUT_HANDLE), &b, 3*sizeof(TCHAR), (LPDWORD)&r, NULL);
    //problem: no returns until enter pressed
    putc(b,stdout);
    while(1)
    {};
}

2 个答案:

答案 0 :(得分:2)

使用SetConsoleMode关闭ENABLE_LINE_INPUT标志。没有行编辑可用,但它不会等到按下Enter键。

请注意,您无法将WCHAR三个int读入{{1}}。

答案 1 :(得分:0)

使用ReadFile / WriteFile考虑Windows中的异步I / O. 见MSDN on asynchronous I/O

这有点复杂,但你确实拥有你想要的东西。

相关问题