views:

96

answers:

1

Assume I have 1Mb file, file pointer is at the beginning of the file. I call synchronous ReadFile:

ReadFile(Handle, Buffer, 1024, Result, nil);

the call is succesful, no error occured. Is it possible that Result value (number of bytes read) is less than 1024 (number of bytes to read)?

I think that is impossible for disk files, I am not sure about other resources that can be accessed by ReadFile. Should I take the above scenario into account while writing a general code that can work with different resources?


To avoid philosophical speculations I can reformulate the question as follows:

Synchronous ReadFile was executed without error and a number of bytes read is less than a number of to read. Can I be sure that EOF is reached?

+2  A: 

In your given scenario, it does seem that for disk files it would be impossible to receive less bytes read than the requested number of bytes.

However, writing general code that can work with different resources, you should not rely on always receiving the requested number of bytes in the situation where position + bytes requested is less than the total number of bytes to be transferred.

For example Readfile can return 0 bytes read on a successful call when the other end of a named pipe called WriteFile with 0 bytes to write...

Marjan Venema
Or a socket or other networked elements.
Larry Osterman
Agreed. ReadFile() explicitally returns the number of bytes actually read. Checking that value and calling ReadFile() again if needed is much more reliable then assuming the output count always matches the requested count.
Remy Lebeau - TeamB