Thread: need of fclose()

  1. #1
    Registered User gaurav9991's Avatar
    Join Date
    Oct 2010
    Location
    Pune, Maharashtra, India
    Posts
    69

    need of fclose()

    i hav read "fclose()
    must be called after file
    operations to avoid unexpected errors."

    but what is need of fclose()?
    What are "unexpeted errors" possible if file is not closed?
    What will happen if a file is opened and not closed. Will it be possible to use that file again without opening?

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Calling fclose will ensure the file descriptor is properly disposed of and output buffers flushed so the data written to the file will be present in the file on disk.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Location
    Las Vegas
    Posts
    66
    fclose() closes the associated file. If function fclose() is not called explicitly, the operating system normally will close the file when program execution terminates. However, if the file is being used for output, some data may still be buffered and not physically written to the output file. It is for this reason that it is important to explicitly call fclose() so that any buffered data is written to the file.

    Failing to properly close a file used for output can result in data loss - this is the "unexpected error" that you should expect!

    Within a single program executing a single time, once a file is opened it can be accessed as needed. If you fail to close the file before the program terminates, the operating system will close the file as part of its housekeeping function. The file can then be opened again.

    Kevin

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by kmess View Post
    Within a single program executing a single time, once a file is opened it can be accessed as needed. If you fail to close the file before the program terminates, the operating system will close the file as part of its housekeeping function. The file can then be opened again.

    Kevin
    I absolutely, without question, would not count on that!

    Closing a file performs multiple functions...
    1) It flushes memory buffers to the disk so the data is actually written out.
    2) It updates the directory information for the file (size, dates, CRC, etc.)
    3) It updates the "archive" status of the file marking it as changed.
    4) It releases the file handle back to the system.

    #4 is the only one you can be certain of if you exit a program with an open file.

    The rule for closing files is the same for freeing memory...
    "Always clean up after yourself".

    To answer gaurav's question... "What are 'unexpected errors"?
    How about total data loss... Especially for files opened with the "w" flag.
    Last edited by CommonTater; 04-25-2011 at 06:56 AM.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    At normal program exit (either via a return from the initial call to main, or if exit() is called), files are guaranteed to be closed properly, which includes flushing. It's therefore not strictly necessary to close files, although it's still a good idea, especially with files opened for writing. By calling fclose() yourself, you can at least be aware that there was a problem, if there was one.

    Abnormal program termination, on the other hand, is not guaranteed to result in properly closed files. The OS will almost certainly clean up the OS-specific bit (such as closing a file descriptor), but the file may not be flushed by the C runtime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fflush/fclose
    By nullifyed in forum C Programming
    Replies: 5
    Last Post: 05-08-2010, 08:32 AM
  2. One help with fclose();
    By sreeramu in forum C Programming
    Replies: 3
    Last Post: 12-03-2007, 02:47 AM
  3. fclose() and EOF
    By Micko in forum C Programming
    Replies: 3
    Last Post: 09-16-2004, 12:53 PM
  4. fclose()
    By GanglyLamb in forum C Programming
    Replies: 4
    Last Post: 11-11-2002, 08:45 AM
  5. close and fclose
    By Ian in forum C Programming
    Replies: 4
    Last Post: 12-13-2001, 05:31 PM