Thread: File IO Error Question

  1. #1
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709

    File IO Error Question

    Just a niggling question:

    Code:
    FILE* f = NULL; 
     
    if ((f = fopen("C:\\test.dat", "w")) == NULL)
    {
    	std::cerr << "Error writing file" << std::endl;
    	exit(1);
    }
     
    // write or whatever
     
    fclose (f);
    If an error occurs, will the file handle be closed?
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If fopen fails, then nothing was opened. Therefore, there's nothing to close. If something else generates an error, it would have to depend on the error I suppose. Usually, no. If you get a read error because you're trying to read past the EOF, no, it doesn't just close the file automatically.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. File IO with .Net SDK and platform SDK
    By AtomRiot in forum Windows Programming
    Replies: 5
    Last Post: 12-14-2004, 10:18 AM