Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Reuse of FILE Variable (http://www.velocityreviews.com/forums/t951030-reuse-of-file-variable.html)

Mike Copeland 08-18-2012 11:25 PM

Reuse of FILE Variable
 
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

s0suk3@gmail.com 08-19-2012 05:00 AM

Re: Reuse of FILE Variable
 
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


s0suk3@gmail.com 08-19-2012 05:02 AM

Re: Reuse of FILE Variable
 
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 :(

army1987 08-19-2012 09:47 AM

Re: Reuse of FILE Variable
 
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/>

James Kanze 08-19-2012 10:59 AM

Re: Reuse of FILE Variable
 
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

Nick Keighley 08-19-2012 12:24 PM

Re: Reuse of FILE Variable
 
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...



All times are GMT. The time now is 09:55 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.