error reading file

This is a discussion on error reading file within the C Programming forums, part of the General Programming Boards category; If I'm trying to read untill end of file, using fgets(), and somewhere inside the loop, let's say fgets fails. ...

  1. #1
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105

    error reading file

    If I'm trying to read untill end of file, using fgets(), and somewhere inside the loop, let's say fgets fails. (Which means we got a read-error).

    What would be the most correct thing to do if that occur?
    Can I try using fgets() again and again, and hope it can work again?
    Or can I try using fgets() again and again at an error, but perhaps with a max-counter?
    Or do I have to stop all reading, and close the file?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You can check feof and ferror to see what's what. If it's eof, then you do whatever you want to do at eof (since that's what was supposed to end your loop anyway, right?) -- if this is common that you get eof in the middle of your loop, then either your file isn't built the way you think it is or your loop test is incorrect (this is surprisingly common). If there's ferror, then I don't know that continuing is possible (I think for sure you would have to do the clear-error function that I've forgot the name of).

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Well, no trying again and again. What if for some reason you cannot read from the file? You will have an infinite loop. A max_counter is a good idea, with the counter reseting maybe when fgets() succeeds?

    There is no correct or wrong thing to do. Trying again after an error or not trying again or how many times to try is completely up to you. Depends on the program and its requirements

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    In my opinion, the correct thing to do is inform the user that there was an error reading the file, then exit the program. It's not really much use trying to reopen and re-read the file since your program has no way of knowing what's wrong.

    Better to tell the user there's a problem. The user can then try to figure out why the file isn't readable and re-run your program if desired.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by brewbuck View Post
    In my opinion, the correct thing to do is inform the user that there was an error reading the file, then exit the program. It's not really much use trying to reopen and re-read the file since your program has no way of knowing what's wrong.

    Better to tell the user there's a problem. The user can then try to figure out why the file isn't readable and re-run your program if desired.
    The general thing to do is:
    1. Check for error
    2. Inform the user, or a log file
    3. Repeat N times

    Now, how many times to repeat depends on the read-errors possible, the time each error needs etc etc. Don't really see why it is correct for you to choose N=0 or N=10 or something else. It depends....

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Is it failing because it's expected to? (Are you reading until it fails, which indicates you've reached the end of the file?) Or is it failing because of something else? It depends on your usage of it really.

    I assume you actually are planning on having some way to tell when you're done reading? If you meet that condition, then you're done, and everything is fine. If not, then you should let them know that something went wrong.

    All of this has been said, and it really is that simple. Basically if it's OK for it to have failed, continue as normal, otherwise, do something else.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    (I think for sure you would have to do the clear-error function that I've forgot the name of)
    You almost had it. clearerr()

    I think a reasonable thing to do would be to see if an error occurred or whether it was just EOF; then you can decide whether to continue the program or exit.

    To be honest, most programs probably just assume the end of normal input is reached and continue execution. (Assuming you didn't read part of a record or something from the file.)
    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.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    In case of a hardware error like disk failure etc. you won't be able to proceed, so examine the error returned before trying ad nauseum.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  4. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

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