Question about fclose

This is a discussion on Question about fclose within the C Programming forums, part of the General Programming Boards category; Where should the fclose function go in this example: Code: FileHandle = fopen(...); if (FileHandle != 0) { fread(...) fread(...) ...

  1. #1
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582

    Question about fclose

    Where should the fclose function go in this example:

    Code:
    FileHandle = fopen(...);
    if (FileHandle != 0)
    {
    	fread(...)
    	fread(...)
    	fread(...)
    	// fclose(FileHandle);
    }
    
    // fclose(FileHandle);
    Where should the fclose instruction go (the top or bottom one commented out)?
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Inside the if statement, although from what I'm reading from various references, if you pass NULL into fclose() nothing horrible will happen.

    This may not be true for all functions that require you to pass a pointer. Know the details about the function you're calling and if passing a NULL pointer will cause issues.

  3. #3
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    Thanks. I wasn't quite sure on that. I thought that, if the file was opened, it had to be closed, even if the file couldn't be found (causing the handle to be zero). If the file didn't exist, it wouldn't then reach the fclose instruction causing a leak (or something like that).
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    fopen() returns NULL upon failure, which means that any FILE struct that it malloc()ed was free()ed before it returned.

    If it wouldn't clean up after itself before returning NULL, then you would have a memory leak that you wouldn't be able to fix because there would be no way of knowing how to deallocate the memory it allocated. If it managed to open the file, but yet return NULL, you would have a similar problem, with no way of knowing how to close the file.

    So this is why, when you receive NULL from fopen(), you can tell there's nothing for you to close and you can simply process the error.

  5. #5
    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.*

  6. #6
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    In other news, FILE pointers (handles) or pointers in general don't always e-null-uate (is that a word, meh I like the way it sounds) to integer zero. Meaning:
    Code:
    fp == 0
    Is not garaunteed to catch a null pointer. What you want is:
    Code:
    fp == NULL
    or if NULL isn't defined:
    Code:
    fp == (char *)0

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    > fp == 0
    > Is not garaunteed to catch a null pointer. What you want is:
    Yes it is.
    http://c-faq.com/null/macsochange.html
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 01:52 PM
  2. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 10:47 PM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  4. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 01:36 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM

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