Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. ReadFile overlapped i\o

ReadFile overlapped i\o

Scheduled Pinned Locked Moved C / C++ / MFC
questionvisual-studiocom
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sawerr
    wrote on last edited by
    #1

    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...

    L _ L 3 Replies Last reply
    0
    • S sawerr

      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...

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • S sawerr

        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...

        _ Offline
        _ Offline
        _Superman_
        wrote on last edited by
        #3

        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 using GetOverlappedResult. Now assume ReadFile returns TRUE, in which case b will be 1. So now we have k with a value of 0 and b with a value of 1, which is a perfect result for an overlapped read. Consider the check if ((b && k) == 0). This translates to if ((1 && 0) == 0). Which further translates to if (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.

        _Microsoft MVP (Visual C++)

        Polymorphism in C

        1 Reply Last reply
        0
        • S sawerr

          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...

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Are you sure that the initial values in your OVERLAPPED structure are correct? Have you checked the error code when ReadFile() returns FALSE? 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.

          C 1 Reply Last reply
          0
          • L Lost User

            Are you sure that the initial values in your OVERLAPPED structure are correct? Have you checked the error code when ReadFile() returns FALSE? 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.

            C Offline
            C Offline
            CPallini
            wrote on last edited by
            #5

            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]

            S 1 Reply Last reply
            0
            • C CPallini

              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]

              S Offline
              S Offline
              sawerr
              wrote on last edited by
              #6

              Thanks for the answers, I got it. How can you advance file pointer if you don't use overlapped IO?

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups