Reuse of FILE Variable

M

Mike Copeland

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
 
S

s0suk3

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.)
 
A

army1987

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

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.
 
J

James Kanze

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.
 
N

Nick Keighley

   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...
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top