Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Reuse of FILE Variable

Reply
Thread Tools

Reuse of FILE Variable

 
 
Mike Copeland
Guest
Posts: n/a
 
      08-18-2012
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
 
Reply With Quote
 
 
 
 
s0suk3@gmail.com
Guest
Posts: n/a
 
      08-19-2012
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

 
Reply With Quote
 
 
 
 
s0suk3@gmail.com
Guest
Posts: n/a
 
      08-19-2012
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
 
Reply With Quote
 
army1987
Guest
Posts: n/a
 
      08-19-2012
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/>
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      08-19-2012
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
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      08-19-2012
On Aug 19, 12:25*am, mrc2...@cox.net (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...

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
To reuse or not to reuse jacob navia C Programming 19 12-18-2006 07:22 AM
code reuse and design reuse sailor.gu@gmail.com C Programming 16 02-12-2006 09:09 PM
Reuse paramter list and reuse connection tshad ASP .Net 5 05-17-2005 12:33 AM
To reuse or not to reuse.... Hylander Java 0 02-26-2004 12:00 AM
shall I reuse a variable/signal or not? walala VHDL 1 09-14-2003 07:46 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57