Thread: fread failing to read full file

  1. #1
    Registered User
    Join Date
    May 2009

    fread failing to read full file


    So this is likely a straight forward issue with my use of either fwrite, or fread. Here is a full listing of my test code, and the output it gives. Sorry for the mingling of microsoft specific "safe" calls.

    int main()
    	short *uArr = new short[50000];
    	short *uComp = new short[50000];
    	for ( int i=0 ; i<50000 ; i++ )
    		uArr[i] = (short)(rand()%0xFFFF);
    	int nWriteCount = 0;
    	FILE* fOut;
    	if ( fopen_s(&fOut, "testWrite", "w") == 0 )
    		nWriteCount = fwrite(uArr,sizeof(short),50000,fOut);
    	printf( "Wrote %d shorts to file.\n", nWriteCount);
    	int nReadCount = 0;
    	FILE* fIn;
    	if ( fopen_s(&fIn, "testWrite", "r") == 0 )
    		nReadCount = fread(uComp,sizeof(short),50000,fIn);
    	printf ( "Read %d shorts from file.\n", nReadCount);
    	delete [] uArr;
    	delete [] uComp;
    And the output:

    Wrote 50000 shorts to file.
    Read 49 shorts from file.
    So it seems like a pretty straightforward task, basically just writing a large array of shorts to file, and then reading them back.

    Of not it fails after reading 49 shorts, so the value where it fails is : -274.

    Thanks for any input.

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Banks of the River Styx
    1) You should be opening the files in binary mode "rb" / "wb".
    2) Seed that random number generator?
    3) You should check your RAND_MAX - if memory serves, on Windows, it is 0x7FFF.

    Of not it fails after reading 49 shorts, so the value where it fails is : -274.
    What do you mean, the value where it fails is...?
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    Since it's a MS OS (I presume, using MS calls, as you mention), I'd expect that the problem lies in that there is a byte of value 0x1A at the 100th byte of the file. So you get EOF at that point, since 0x1A is the same as CTRL-Z, which in older OS's that didn't have exact byte-counts for the file-size would use to indicate "the rest of the file is not there" when it got part-way through the final block of the file.

    Try adding a "b" to your mode when you open the file, and I'm sure it will be better.

    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    May 2009
    Thanks alot guys. The issue was my not opening in binary mode.

    And as pointed out the actual point where it stopped was after reading a short 0x1A.

    Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. archive format
    By Nor in forum A Brief History of
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
Website Security Test