Win32 select/poll/eof/ANYTHING?
Asked Answered
T

3

6

Using the standard Win32 file I/O API's (CreateFile/ReadFile/etc), I'm trying to wait for a file to become readable, or for an exception to occur on the file. If Windows had any decent POSIX support, I could just do:

select(file_count, files_waiting_for_read, NULL, files_waiting_for_excpt, NULL, NULL);

And select will return when there's anything interesting on some of the files. Windows doesn't support select or poll. Fine. I figured I could take the file and do something like:

while(eof(file_descriptor))
{
    Sleep(100);
}

The above loop would exit when more data is available to be read. But nope, Windows doesn't have an equivalent of eof() either! I could possibly call ReadFile() on the file, and determine if it's at the eof that way. But, then I'd have to handle the reading at that point in time -- I'm hoping to simply be able to figure out that a file is readable, without actually reading it.

What are my options?

Transonic answered 28/5, 2010 at 3:2 Comment(0)
U
8

Windows has a completely different architecture for asynchronous I/O. You will need to use overlapped I/O with or without the related I/O completion ports.

Note that the standard Winsock interface does have a POSIX-like select() function, but that only works with network sockets.

Unlace answered 28/5, 2010 at 3:7 Comment(4)
Wow I hate Windows. Everything that should be simple isn't. Is there really no 'GetEndOfFile' type of function? I see there's a SetEndOfFile :(Transonic
@Andrew, Windows is not POSIX compliant.Bearish
Different doesn't mean inferior; obviously with one model some things may be simpler and other more complicated. You just have to think about the same thing from a different perspective.Gametophyte
You might want to look at GetFileSize: msdn.microsoft.com/en-us/library/aa364955(VS.85).aspx to find the end of file. First hit on a search for "get file size".Regalia
D
1

I am answering six year later here goes anyway: I found WSAPoll similar to *nix poll. Here link to MSDN: https://msdn.microsoft.com/en-us/library/windows/desktop/ms741669(v=vs.85).aspx Iwas added to Vista and later versions and works with sockets.

Dyna answered 12/8, 2016 at 19:16 Comment(0)
P
1

It's been over a decade but I just need to correct you about something: regular (disk) files are always ready to read and write according to POSIX, so select or poll will simply return immediately even though a subsequent call to read() or write() may block or fail (similarly, O_NONBLOCK does nothing for disk files).

Regular files shall always poll TRUE for reading and writing.

https://pubs.opengroup.org/onlinepubs/009696799/functions/poll.html

Windows has always provided first-class asynchronous I/O facilities for regular files, with various approaches depending on your application type. In only the past handful of years are Unix-based OSes starting to catch up (Linux with io_uring, FreeBSD with the only true async kernel-based POSIX AIO support).

also eof() is not posix, but MSVCRT actually does have that function (as _eof) in io.h which exists for Unix compatibility.

Parenteau answered 23/2, 2023 at 6:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.