Limits on _read

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • emaghero
    New Member
    • Oct 2006
    • 85

    Limits on _read

    Evening all,
    I have a quick question about the _read function. I am trying to read in a large data set into a character buffer. The data is stored in binary format. The size of the input data file is approximately 7 Mb, I have declared that the buffer should be 10 Mb. The code used is below, I am using MSVS 2003 in C++.
    Code:
    char BUF[1024*1024*10];//10 Mb buffer
    
    int main(){
    		  
    		 char *buf=BUF;//pointer to the buffer
    		 
    		 int file=_open("file.dat",_O_RDONLY,_S_IREAD);
    
    		 if(file==-1){
    					perror("Failed to open file");
    		 }
    		 else{
    				cout<<"File is open\n";
    
    				int bytes;
    				unsigned int nbytes=(1024*1024*10);
    
    			   bytes=_read(file,(void *)buf,nbytes);   
    		 }
    }
    This code works to some extent. However, no matter what value I give for nbytes, the variable bytes always returns 112. This is causing me some concern as it is evident that all the data is not being read into the buf. Whenever I count into the buffer beyond a certain value of bytes I cannot access the data that I know should be there.

    Is there a limit to the size of a file that can be read into a buffer using _read?

    Can anybody suggest a solution to the problem?
  • RRick
    Recognized Expert Contributor
    • Feb 2007
    • 463

    #2
    Check how you are opening the file. You can open a windows file as binary or text. Make sure it is opened as binary. If its opened as a text file, Ctl-Z will act as an EOF. This sounds like what is happening.

    Check this link for more info:
    http://msdn2.microsoft .com/en-us/......../en-us/library/wyssk1bs(VS.80) .aspx
    Last edited by sicarie; Sep 6 '07, 07:08 PM. Reason: Making it a hotlink................so, link, how _you_ doin?

    Comment

    • emaghero
      New Member
      • Oct 2006
      • 85

      #3
      Originally posted by RRick
      Check how you are opening the file. You can open a windows file as binary or text. Make sure it is opened as binary. If its opened as a text file, Ctl-Z will act as an EOF. This sounds like what is happening.

      Check this link for more info:
      http://msdn2.microsoft .com/en-us/library/wyssk1bs(VS.80) .aspxhttp://msdn2.microsoft .com/en-us/library/wyssk1bs(VS.80) .aspx
      Unfortunately that didn't work. It was suggested to me that I should try to read it in multiple times to different locations in the buffer each time. Would this alleviate the problem?

      Comment

      • RRick
        Recognized Expert Contributor
        • Feb 2007
        • 463

        #4
        Probably. I'm raising a caution flag here because you're not fixing the problem, but circumventing it. _read appears to have a mind of its own, and can ignore/skip some characters. With a binary file, this is bad, crash and burn bad.

        That said, you can loop _read until you get an EOF. You can also place all of the data in a single buffer by updating a buffer pointer with the number of read bytes read and passing that pointer back to _read.

        If you plan to go this route, I would check it with an ascii file with lots of <cr><lf>. _read will keep one but not both characters. Check and see if the lengths of the file and buffer are the same. If not, then you're back to the open as text instead of binary.

        Comment

        Working...