Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nonblocking read from stdin

I expect ReadConsoleW() to return after reading a specific number of bytes. But it doesn't return.

How can I make ReadConsoleW() return as soon as it finished reading the number of bytes specified?

The code I tried is here:

#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)
    {};
}
like image 319
kim taeyun Avatar asked Jun 04 '26 05:06

kim taeyun


1 Answers

Use SetConsoleMode to turn off ENABLE_LINE_INPUT flag. No line editing will be available, but it won't wait until the Enter is pressed.

Note that you can't read three WCHARs into an int.

like image 53
Anton Kovalenko Avatar answered Jun 06 '26 22:06

Anton Kovalenko