开发者

How to read available input without blocking on Windows

开发者 https://www.devze.com 2023-01-14 05:55 出处:网络
On Linux, I can read available input without blocking the process: fcntl(STDIN_FILENO, F_SETFL, fcntl(STDIN_FILENO, F_GETFL, 0) | O_NONBLOCK )

On Linux, I can read available input without blocking the process:

fcntl(STDIN_FILENO, F_SETFL, fcntl(STDIN_FILENO, F_GETFL, 0) | O_NONBLOCK )
char buf[开发者_JAVA百科n];
int r = fread(buf, 1, n, stdin);
if (r == 0){
    printf("nothing\n");
}
else {
    printf("read: ");
    fwrite(buf, 1, r, stdout);
    printf("\n");
}

The input origin can be anything, such as a file, a terminal or a pipe.

How can I do it on Windows XP?

Thanks.


Why not read the input from a second thread? Depending on your situation, it might be a much easier approach, instead of using non-blocking IO's.


You can achieve this on Windows by passing FILE_FLAG_OVERLAPPED to CreateFile(). It doesn't quite look the same as Linux and there may be some slight differences but it achieves the same thing.

Take a look at the MSDN page on Synchronous vs. Asynchronous IO which provides you with even more detail on the various options.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号