Reuse of FILE Variable

Discussion in 'C++' started by Mike Copeland, Aug 19, 2012.

  1. I'm curious about the best way to handle a file which is opened and
    closed repeatedly throughout a program's execution. A file (FILE *pfv1)
    is used to write one or more different files during the program's
    execution, and it can be opened and closed many times - with different
    file names.
    My question involves the advisability of setting the file variable to
    NULL each time it's closed, as well as using that condition to dictate
    subsequent open processing. My guess is that doing so will proliferate
    multiple instances of the file structure as each open is executed (and
    wasting runtime memory), whereas _not_ assigning it a NULL address will
    reuse the structure during the multiple opens (for a different file's
    processing). Is this true?
    Note that each open/write/close sequence is discrete and file-
    specific. During processing the program may well detect that the output
    file exists and will offer the user option of appending to or
    overwriting the file's content.
    In summary, I want to be able to use a single file multiple times for
    writing content to different files, because the logic to write data is
    not specific to the output file(s) being produced.
    Please advise. TIA
    Mike Copeland, Aug 19, 2012
    #1
    1. Advertising

  2. Mike Copeland

    Guest

    On Saturday, August 18, 2012 6:25:21 PM UTC-5, Mike Copeland wrote:
    > I'm curious about the best way to handle a file which is opened and
    >
    > closed repeatedly throughout a program's execution. A file (FILE *pfv1)
    >
    > is used to write one or more different files during the program's
    >
    > execution, and it can be opened and closed many times - with different
    >
    > file names.
    >


    I suggest not using FILE as it has a bad design. Use std::basic_[i|o]fstream or the file classes found in many general-purpose C++ classes. The most important thing in these classes is polymorphic streams.

    > My question involves the advisability of setting the file variable to
    >
    > NULL each time it's closed, as well as using that condition to dictate
    >
    > subsequent open processing. My guess is that doing so will proliferate
    >
    > multiple instances of the file structure as each open is executed (and
    >
    > wasting runtime memory), whereas _not_ assigning it a NULL address will
    >
    > reuse the structure during the multiple opens (for a different file's
    >
    > processing). Is this true?
    >


    No. Closing a file will release the memory used for it and make it available for subsequent allocations. You will reassign the FILE* variable anyway everytime you open a file:

    FILE* file = fopen("1.dat");
    // use file
    fclose(file);
    file = fopen("2.dat"); // re-assignment
    // use file
    fclose(file);

    So assigning NULL after closing is simply a matter of whether you need to test if the file is open (which you said you did).

    With std::basic_[i|o]fstream, you wouldn't use a pointer but a direct object. You can do multiple open/close sequences with the same object, and you can test whether it is open with the is_open method:

    wfstream file;
    file.open(L"1.dat");
    // use file
    file.close();
    // ...
    if (!file.is_open())
    {
    file.open(L"2.dat");
    // use file
    file.close();
    }

    > Note that each open/write/close sequence is discrete and file-
    >
    > specific. During processing the program may well detect that the output
    >
    > file exists and will offer the user option of appending to or
    >
    > overwriting the file's content.
    >
    > In summary, I want to be able to use a single file multiple times for
    >
    > writing content to different files, because the logic to write data is
    >
    > not specific to the output file(s) being produced.
    >


    That's not really possible, but it doesn't mean that you'll waste memory, have memory leaks, or anything like that. (With std::basic_[i|o]fstream, youWILL use the same file object multiple times, but the underlying OS file object/descriptor will be re-allocated for each new file you open.)

    > Please advise. TIA
    , Aug 19, 2012
    #2
    1. Advertising

  3. Mike Copeland

    Guest

    On Sunday, August 19, 2012 12:00:10 AM UTC-5, (unknown) wrote:
    > Use std::basic_[i|o]fstream or the file classes found in many general-purpose C++ classes.


    I meant libraries :(
    , Aug 19, 2012
    #3
  4. Mike Copeland

    army1987 Guest

    Nothing of what you asked is specific to C++, so you might want to ask
    comp.lang.c instead.

    On Sat, 18 Aug 2012 16:25:21 -0700, Mike Copeland wrote:

    > My question involves the advisability of setting the file variable to
    > NULL each time it's closed, as well as using that condition to dictate
    > subsequent open processing. My guess is that doing so will proliferate
    > multiple instances of the file structure as each open is executed (and
    > wasting runtime memory), whereas _not_ assigning it a NULL address will
    > reuse the structure during the multiple opens (for a different file's
    > processing). Is this true?


    No. fopen() doesn't know what you do with your pointers.



    --
    [ T H I S S P A C E I S F O R R E N T ]
    Troppo poca cultura ci rende ignoranti, troppa ci rende folli.
    -- fathermckenzie di it.cultura.linguistica.italiano
    <http://xkcd.com/397/>
    army1987, Aug 19, 2012
    #4
  5. Mike Copeland

    James Kanze Guest

    On Sunday, August 19, 2012 12:25:21 AM UTC+1, Mike Copeland wrote:
    > I'm curious about the best way to handle a file which is opened and
    > closed repeatedly throughout a program's execution. A file (FILE *pfv1)
    > is used to write one or more different files during the program's
    > execution, and it can be opened and closed many times - with different
    > file names.


    > My question involves the advisability of setting the file variable to
    > NULL each time it's closed, as well as using that condition to dictate
    > subsequent open processing. My guess is that doing so will proliferate
    > multiple instances of the file structure as each open is executed (and
    > wasting runtime memory), whereas _not_ assigning it a NULL address will
    > reuse the structure during the multiple opens (for a different file's
    > processing). Is this true?


    > Note that each open/write/close sequence is discrete and file-
    > specific. During processing the program may well detect that the output
    > file exists and will offer the user option of appending to or
    > overwriting the file's content.


    > In summary, I want to be able to use a single file multiple times for
    > writing content to different files, because the logic to write data is
    > not specific to the output file(s) being produced.


    fopen returns a pointer; it doesn't take one, so there is no possible
    way it could change its behavior depending on the contents of that
    pointer. Any resources used by an open file will be pointed to by the
    return value of fopen, and will be freed by fclose. You can copy the
    pointer, etc., and it will always point to the same open file.

    But all of this is fundamental to what a pointer is. I'd suggest that
    you start by learning that.

    --
    James Kanze
    James Kanze, Aug 19, 2012
    #5
  6. On Aug 19, 12:25 am, (Mike Copeland) wrote:
    >    I'm curious about the best way to handle a file which is opened and
    > closed repeatedly throughout a program's execution.  A file (FILE *pfv1)
    > is used to write one or more different files during the program's
    > execution, and it can be opened and closed many times - with different
    > file names.
    >    My question involves the advisability of setting the file variableto
    > NULL each time it's closed, as well as using that condition to dictate
    > subsequent open processing.  My guess is that doing so will proliferate
    > multiple instances of the file structure as each open is executed (and
    > wasting runtime memory), whereas _not_ assigning it a NULL address will
    > reuse the structure during the multiple opens (for a different file's
    > processing).  Is this true?
    >    Note that each open/write/close sequence is discrete and file-
    > specific.  During processing the program may well detect that the output
    > file exists and will offer the user option of appending to or
    > overwriting the file's content.
    >    In summary, I want to be able to use a single file multiple times for
    > writing content to different files, because the logic to write data is
    > not specific to the output file(s) being produced.


    this being C++ I'd suggest using a class to do this management. So you
    could wrap FILE* in a class or even use an iostream...
    Nick Keighley, Aug 19, 2012
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. walala
    Replies:
    1
    Views:
    476
  2. tshad
    Replies:
    5
    Views:
    518
    Steve C. Orr [MVP, MCSD]
    May 17, 2005
  3. Hylander

    To reuse or not to reuse....

    Hylander, Feb 26, 2004, in forum: Java
    Replies:
    0
    Views:
    412
    Hylander
    Feb 26, 2004
  4. code reuse and design reuse

    , Feb 7, 2006, in forum: C Programming
    Replies:
    16
    Views:
    1,005
    Malcolm
    Feb 12, 2006
  5. jacob navia

    To reuse or not to reuse

    jacob navia, Nov 5, 2006, in forum: C Programming
    Replies:
    19
    Views:
    515
    Dave Thompson
    Dec 18, 2006
Loading...

Share This Page