Reasons as to why fread would fail

This is a discussion on Reasons as to why fread would fail within the C Programming forums, part of the General Programming Boards category; Here is a piece of code : Code: if ((t = (fread(buffer,BLOCK_SIZ,1,diskp))) != 1) // Read from disk to buffer ...

  1. #1
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625

    Reasons as to why fread would fail

    Here is a piece of code :

    Code:
    if ((t = (fread(buffer,BLOCK_SIZ,1,diskp))) != 1)  // Read from disk to buffer
    {
    	if(feof(diskp) != 0)
    	{
    		fprintf(stdout,"End of file reached. %d Blocks read.\n",f);
    		return t;
    	}
    	
    	if((err_num = ferror(diskp)) != 0)
    	{
    		fprintf(stderr,"%s\n",strerror(err_num));
    	}
    
    	fprintf(stderr,"Error in disk read : Buffer write failed. %d\n",f);
    	return 0;
    }
    Here's my problem. Often, I'm calling this piece of code to read a block of data from a file pointed to by diskp. Now on certain occasions (namely, when diskp is opened with "r+b" permission), fread fails (and reads 0 elements) and it therefore gets into the if statement. Furthermore, everytime it fails it always outputs "Error in disk read : Buffer write failed". This is where I'm confused. How can my program get so far ? From what I grasp from fread's man page, it should fail if it reaches EOF or gets an error (in which case ERRNO is set). But I believe I've testing both of these, but neither of these conditions corresponding to what my program is telling me. So how come fread is failing ? Is there some other, "alternate" method for fread to fail or something ?

    Thanks in advance.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    > Is there some other, "alternate" method for fread to fail or something ?
    Yes.

    > if ((t = (fread(buffer,BLOCK_SIZ,1,diskp))) != 1)
    If say your file is 120 bytes long, and BLOCK_SIZ is say 100 then the first fread will read a 100 byte block and return 1.
    The next fread will not find a "complete" 100 byte block, so won't read any of it (feof() remains false), and fread will return 0.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    So assuming you're in the context of a binary file, if you seek to a location bytes-1, then call fread to read a block of size bytes, instead of reading the bytes-1, and then signlaing EOF, it won't read anything and return 0 ?

    Do you have any idea of the reason it was implemented in such a fashion ? I know I don't quite see why they'd do it that way...
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    fread() was intended for reading data structures (and other fixed-size entities) from files. If there isn't enough data in the file to form one structure, it makes sense to fail the read.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Non-blocking socket connection problem
    By cbalu in forum Linux Programming
    Replies: 25
    Last Post: 06-03-2009, 03:15 AM
  2. fread problems or memory problems
    By Lechuza in forum C Programming
    Replies: 1
    Last Post: 03-22-2009, 01:45 PM
  3. Why is fread sometimes taking so long?
    By manugarciac in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2007, 12:25 AM
  4. fread item_size limit
    By nvoigt in forum C++ Programming
    Replies: 2
    Last Post: 03-30-2005, 09:08 AM
  5. For what reasons would connect() fail?
    By Inquirer in forum C++ Programming
    Replies: 0
    Last Post: 11-29-2002, 04:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21