Re: temporary file in C++

From: B. v Ingen Schenau (bart_at_ingen.ddns.info)
Date: 04/30/04


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/


Relevant Pages

  • Re: CDR quality / reliability problems
    ... I've seen other cases where I knew far more about a subject than Scott ... On the case of the NPR satellite system, I was trying to learn the answers ... did not jive with the information I was reading in the CDSpeed FAQ. ... I was seeing were due to one of the other 4 reasons. ...
    (rec.audio.pro)
  • Re: forward declaration error
    ... Scott wrote: ... > includes C.h which itself includes B.h - the file I am calling foo.h from. ... the circular dependency by moving some of the code to a different file. ... a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq ...
    (alt.comp.lang.learn.c-cpp)
  • Re: CDR quality / reliability problems
    ... for since Scott was not discrediting you, ... did not jive with the information I was reading in the CDSpeed FAQ. ... I was seeing were due to one of the other 4 reasons. ...
    (rec.audio.pro)
  • Re: how to patch
    ... On Thu, 11 Aug 2005, Scott wrote: ... > where can i find a site that tell how to install a ".patch" ... Start with Section~4.11 of the FAQ, and if you have problems, ask more ...
    (rec.games.roguelike.nethack)