ReadFile overlapped i\o
-
Hi I am using ReadFile with overlapped i\o in synchronous mode. I have 2 questions.
while (true) { BOOL b = ReadFile(hSource, &buffer, 200, &k, &ov); if ((b && k ) == 0) { break; } ov.Offset = ov.Offset + k; } }
The file that program reads is bigger than 200 byte so it must not return EOF. But it does. When it reads first phase b = 0 (TRUE), k == 0 which means synchronous read operation gets to the end of a file as described here: http://msdn.microsoft.com/en-us/library/aa365690%28v=vs.85%29.aspx[^] My first question is, why does program return EOF even though file 6KB. My second question is, I want to read file in a loop as seen here: ov.Offset = ov.Offset + k; But i don't understand what is the differences between offset and offsethigh. I mean which one must be increased? I don't really understand the effect of offset and offsethigh. First i thought offset must be beginning and offsethigh is the ending so i must advanced both of them but i think this is not true. Thanks...
-
Hi I am using ReadFile with overlapped i\o in synchronous mode. I have 2 questions.
while (true) { BOOL b = ReadFile(hSource, &buffer, 200, &k, &ov); if ((b && k ) == 0) { break; } ov.Offset = ov.Offset + k; } }
The file that program reads is bigger than 200 byte so it must not return EOF. But it does. When it reads first phase b = 0 (TRUE), k == 0 which means synchronous read operation gets to the end of a file as described here: http://msdn.microsoft.com/en-us/library/aa365690%28v=vs.85%29.aspx[^] My first question is, why does program return EOF even though file 6KB. My second question is, I want to read file in a loop as seen here: ov.Offset = ov.Offset + k; But i don't understand what is the differences between offset and offsethigh. I mean which one must be increased? I don't really understand the effect of offset and offsethigh. First i thought offset must be beginning and offsethigh is the ending so i must advanced both of them but i think this is not true. Thanks...
Hi, I'm only handling the second question: the file offset once was simply a 32-bit number; later on it had to become a 64-bit number (in order to handle very large files), and MS has chosen to represent such 64-bit number as a struct containing two 32-bit numbers called xyzHigh and xyz. Their combined value equals
(xyzHigh<<32)+xyz
, so what you need to do is add to the lower part, and if an overflow occurs, add that to the higher part. :)Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
Hi I am using ReadFile with overlapped i\o in synchronous mode. I have 2 questions.
while (true) { BOOL b = ReadFile(hSource, &buffer, 200, &k, &ov); if ((b && k ) == 0) { break; } ov.Offset = ov.Offset + k; } }
The file that program reads is bigger than 200 byte so it must not return EOF. But it does. When it reads first phase b = 0 (TRUE), k == 0 which means synchronous read operation gets to the end of a file as described here: http://msdn.microsoft.com/en-us/library/aa365690%28v=vs.85%29.aspx[^] My first question is, why does program return EOF even though file 6KB. My second question is, I want to read file in a loop as seen here: ov.Offset = ov.Offset + k; But i don't understand what is the differences between offset and offsethigh. I mean which one must be increased? I don't really understand the effect of offset and offsethigh. First i thought offset must be beginning and offsethigh is the ending so i must advanced both of them but i think this is not true. Thanks...
I believe the way you're making the check is not right. For an overlapped operation, no. of bytes read, k in this case will always be
0
. The actual bytes read is taken usingGetOverlappedResult
. Now assumeReadFile
returnsTRUE
, in which case b will be1
. So now we have k with a value of0
and b with a value of1
, which is a perfect result for an overlapped read. Consider the checkif ((b && k) == 0)
. This translates toif ((1 && 0) == 0)
. Which further translates toif (0 == 0)
. Now this becomes true and the program thinks it has reached EOF. Here is an excerpt from the documentation on how to check for EOF in a overlapped read operation - if the file is opened with FILE_FLAG_OVERLAPPED and lpOverlapped is not NULL, the return value is zero (0) and GetLastError returns ERROR_HANDLE_EOF when the file pointer goes beyond the current end of file«_Superman_» _I love work. It gives me something to do between weekends.
-
Hi I am using ReadFile with overlapped i\o in synchronous mode. I have 2 questions.
while (true) { BOOL b = ReadFile(hSource, &buffer, 200, &k, &ov); if ((b && k ) == 0) { break; } ov.Offset = ov.Offset + k; } }
The file that program reads is bigger than 200 byte so it must not return EOF. But it does. When it reads first phase b = 0 (TRUE), k == 0 which means synchronous read operation gets to the end of a file as described here: http://msdn.microsoft.com/en-us/library/aa365690%28v=vs.85%29.aspx[^] My first question is, why does program return EOF even though file 6KB. My second question is, I want to read file in a loop as seen here: ov.Offset = ov.Offset + k; But i don't understand what is the differences between offset and offsethigh. I mean which one must be increased? I don't really understand the effect of offset and offsethigh. First i thought offset must be beginning and offsethigh is the ending so i must advanced both of them but i think this is not true. Thanks...
Are you sure that the initial values in your
OVERLAPPED
structure are correct? Have you checked the error code whenReadFile()
returnsFALSE
? Lastly, are you sure it is a good idea to use overlapped IO for what is such a simple file read?I must get a clever new signature for 2011.
-
Are you sure that the initial values in your
OVERLAPPED
structure are correct? Have you checked the error code whenReadFile()
returnsFALSE
? Lastly, are you sure it is a good idea to use overlapped IO for what is such a simple file read?I must get a clever new signature for 2011.
Richard MacCutchan wrote:
Lastly, are you sure it is a good idea to use overlapped IO for what is such a simple file read?
Good point, indeed. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Richard MacCutchan wrote:
Lastly, are you sure it is a good idea to use overlapped IO for what is such a simple file read?
Good point, indeed. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]