Re: temporary file in C++
From: B. v Ingen Schenau (bart_at_ingen.ddns.info)
Date: 04/30/04
- Next message: Robert W Hand: "Re: [C++] Vectors & Dynamic allocation"
- Previous message: David White: "Re: Newbie OOP question"
- In reply to: Scott: "temporary file in C++"
- Next in thread: Scott: "Re: temporary file in C++"
- Reply: Scott: "Re: temporary file in C++"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 30 Apr 2004 11:02:02 +0200
Scott wrote:
> Hi All,
>
> Is there a way of creating a temporary file in C++ (similar to the
> tmpfile() in C)?
Not one that is integrated into IOStreams.
C++ of course inherited tmpfile() from C.
> I have a C++ object that streams output to an fstream,
> and I would like that file to be temporary (i.e. deleted after it is
> used). What the program is doing is writing data to a file, and then
> converting that file to another file using functions in a shared library
> (which only accepts an existing file for input). The first file is now
> unnecessary, and I would like to delete it. Any suggestions or help would
> be most appreciated.
Your options depend a bit on how the 3rd-party library has to be told about
the file it must use.
If the library accepts an (i)stream reference, then you can use a
stringstream instead of an fstream. stringstreams store their data in
memory instead of in a file, but otherwise they provide (nearly) the same
interface to their users.
If the library has to be passed the name of a file, you have no other option
than to create a file, fill it with data and after the library is finished
with it, remove the file.
If multiple copies of your program might be executed at the same time on the
same computer, you can use tmpnam() to create a unique file name.
Removing such a temporary file can be done with remove().
Both those functions come originally from C, but they can be used just as
good in C++.
>
> Best,
> Scott
Bart v Ingen Schenau
-- a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq c.l.c FAQ: http://www.eskimo.com/~scs/C-faq/top.html c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/
- Next message: Robert W Hand: "Re: [C++] Vectors & Dynamic allocation"
- Previous message: David White: "Re: Newbie OOP question"
- In reply to: Scott: "temporary file in C++"
- Next in thread: Scott: "Re: temporary file in C++"
- Reply: Scott: "Re: temporary file in C++"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|