Topic: temporary file with fstream
Author: Salters <msalters@lucent.com>
Date: 1999/06/07 Raw View
Ross Smith wrote:
>
> Siemel Naran wrote:
> >
> > On 27 May 1999 16:22:04 GMT, Michele Paoli <zizzi@cribecu.sns.it> wrote:
> > >
> > >For C-code I use tmpfile() C-function to create temporary files.
> > >What can i do with fstream?
> >
> > The function tmpfile() is neither part of C nor C++. But it appears
> > to be standard on UNIX and Linux. Look the man page of tmpfile (by
> > typing "man tmpfile") and you'll see hints about other functions:
> > mktemp, mkstemp, tmpnam, tempnam.
> To answer Michele's original question: Use tmpnam() to get a file name,
> then use that to open an iostream. After you're done, close the stream
> and remove() the file. Put all this in a class so it happens
> automatically.
>
I remember reading somewhere that the correct way to get a unique temporary
file was to use tmpfile(), since an application may return the same value
of tmpnam twice if no file with that name is created after the first but
before the second call. Clearly, in a preemptive multitasking system this is
to weak a guarantee.
Unfortunately, I don't have a C standard here, and I believe any
multitasking issues like this are not handled in it. Anybody ?
Michiel Salters
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: comeau@panix.com (Greg Comeau)
Date: 1999/06/08 Raw View
In article <375BA77E.4C71089D@lucent.com> Salters <msalters@lucent.com> writes:
>I remember reading somewhere that the correct way to get a unique temporary
>file was to use tmpfile(), since an application may return the same value
>of tmpnam twice if no file with that name is created after the first but
>before the second call. Clearly, in a preemptive multitasking system this is
>to weak a guarantee.
>Unfortunately, I don't have a C standard here, and I believe any
>multitasking issues like this are not handled in it. Anybody ?
Right, tmpname() only generates a valid file name, it does not "create" one.
tmpfile() does create one (a binary file with wb+ mode, that will
automatically be removed (though it's not clear that's the same as being
remove()d). I do not know how it is supposed to interact with a
preemptive multitasking system in C or C++.
- Greg
--
Comeau Computing, 91-34 120th Street, Richmond Hill, NY, 11418-3214
Producers of Comeau C/C++ 4.2.38 -- New Release! We now do Windows too.
Email: comeau@comeaucomputing.com / Voice:718-945-0009 / Fax:718-441-2310
*** WEB: http://www.comeaucomputing.com ***
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: James.Kanze@dresdner-bank.com
Date: 1999/05/31 Raw View
In article <MPG.11b7c25df999e85d989b05@news.mindspring.com>,
jcoffin@taeus.com (Jerry Coffin) wrote:
> In article <slrn7kqvhd.jdf.sbnaran@bardeen.ceg.uiuc.edu>,
> sbnaran@bardeen.ceg.uiuc.edu says...
>
> [ ... ]
>
> > The function tmpfile() is neither part of C nor C++.
>
> Section 7.9.4.3 of the C standard appears to disagree.
>
> > But it appears
> > to be standard on UNIX and Linux. Look the man page of tmpfile (by
> > typing "man tmpfile") and you'll see hints about other functions:
> > mktemp, mkstemp, tmpnam, tempnam.
>
> I suspect the standard lacks the requested functionality simply
> because it can be synthesized (fairly) easily from existing classes.
This depends. Generally, although it is a quality of implementation
issue, I would expect a good implementation to take steps so that the
file is deleted even if the program doesn't terminate normally. Under
UNIX, there is a rather simple hack to achieve this: once you have
created and opened the file, you delete it. If there are no more
directory entries referring to the file, the system deletes it when it
is closed. Under other systems, there may be a specific system call to
achieve the same effect. This functionality is *not* accessible in a
portable manner.
--
James Kanze mailto:
James.Kanze@dresdner-bank.com
Conseils en informatique orientie objet/
Beratung in objekt orientierter
Datenverarbeitung
Ziegelh|ttenweg 17a, 60598 Frankfurt, Germany Tel. +49 (069) 63 19 86
27
Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: jcoffin@taeus.com (Jerry Coffin)
Date: 1999/05/29 Raw View
In article <slrn7kqvhd.jdf.sbnaran@bardeen.ceg.uiuc.edu>,
sbnaran@bardeen.ceg.uiuc.edu says...
[ ... ]
> The function tmpfile() is neither part of C nor C++.
Section 7.9.4.3 of the C standard appears to disagree.
> But it appears
> to be standard on UNIX and Linux. Look the man page of tmpfile (by
> typing "man tmpfile") and you'll see hints about other functions:
> mktemp, mkstemp, tmpnam, tempnam.
I suspect the standard lacks the requested functionality simply
because it can be synthesized (fairly) easily from existing classes.
If tmpfile was really commonly used in C, it might have been worth
putting an equivalent in the iostreams library as well. If, however,
the committee were entertaining the possibility of adding more classes
to the library, I can think of quite a few I think I'd rather see
added first.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: brahms@mindspring.com (Stan Brown)
Date: 1999/05/30 Raw View
Dixitque sbnaran@bardeen.ceg.uiuc.edu (Siemel Naran) in comp.std.c++:
>
>On 27 May 1999 16:22:04 GMT, Michele Paoli <zizzi@cribecu.sns.it> wrote:
>>For C-code I use tmpfile() C-function to create temporary files.
>>What can i do with fstream?
>
>The function tmpfile() is neither part of C nor C++.
Siemel mistyped "part of both C and C++". See ANSI C 4.9.4.3 (which is
subclause 7.9.4.3 in the ISO standard).
Michele, tmpfile() does two things: creates a file and connects it to a
FILE*. For C++ you want only the first of those, so use tmpnam() to
generate a name and then open it for writing using normal stream
operations.
--
There's no need to e-mail me a copy of a follow-up; but if you do,
please identify it as such.
Stan Brown, Oak Road Systems, Cleveland, Ohio, USA
http://www.mindspring.com/~brahms/
My reply address is correct as is. The courtesy of providing a correct
reply address is more important to me than time spent deleting spam.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Darron Shaffer <darron.shaffer@beasys.com>
Date: 1999/05/30 Raw View
Ross Smith <ross.s@ihug.co.nz> writes:
> Siemel Naran wrote:
> >
> > On 27 May 1999 16:22:04 GMT, Michele Paoli <zizzi@cribecu.sns.it> wrote:
> > >
> > >For C-code I use tmpfile() C-function to create temporary files.
> > >What can i do with fstream?
> >
> > The function tmpfile() is neither part of C nor C++. But it appears
> > to be standard on UNIX and Linux. Look the man page of tmpfile (by
> > typing "man tmpfile") and you'll see hints about other functions:
> > mktemp, mkstemp, tmpnam, tempnam.
>
> tmpfile() and tmpnam() are required by the C, and by inclusion C++,
> standards.
>
> To answer Michele's original question: Use tmpnam() to get a file name,
> then use that to open an iostream. After you're done, close the stream
> and remove() the file. Put all this in a class so it happens
> automatically.
>
BUT: If you are using Visual C++ 5.x (And perhaps 6.x) keep in mind
that both tmpfile() and tmpnam() will produce a file in the root
directory of the *current drive*. If you don't have permissions or
space in that directory, your application may fail.
Unix system usually allow you to control the directory by using an
environment variable.
This is, of course, conforming. A Quality of Implementation issue.
Darron
--
__ __ _ Enterprise Middleware Solutions Darron J Shaffer
_ ) ___ _\ BEA Systems Inc. Sr. Software Engineer
__) __ \ 4965 Preston Park Blvd, Ste 500 darron.shaffer@beasys.com
Plano, TX 75093 Voice: (972) 943-5137
http://www.beasys.com Fax: (972) 943-5111
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Michele Paoli <zizzi@cribecu.sns.it>
Date: 1999/05/27 Raw View
Hi all,
For C-code I use tmpfile() C-function to create temporary files.
What can i do with fstream?
Thanks.
Zizzi.
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: sbnaran@bardeen.ceg.uiuc.edu (Siemel Naran)
Date: 1999/05/27 Raw View
On 27 May 1999 16:22:04 GMT, Michele Paoli <zizzi@cribecu.sns.it> wrote:
>Hi all,
>
>For C-code I use tmpfile() C-function to create temporary files.
>What can i do with fstream?
The function tmpfile() is neither part of C nor C++. But it appears
to be standard on UNIX and Linux. Look the man page of tmpfile (by
typing "man tmpfile") and you'll see hints about other functions:
mktemp, mkstemp, tmpnam, tempnam.
--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Matt Seitz" <mseitz@meridian-data.com>
Date: 1999/05/28 Raw View
Siemel Naran <sbnaran@bardeen.ceg.uiuc.edu> wrote in message
news:slrn7kqvhd.jdf.sbnaran@bardeen.ceg.uiuc.edu...
> The function tmpfile() is neither part of C nor C++.
Hmm, P. J. Plauger and Jim Broidie include tmpfile() in their book, STANDARD
C.
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Matt Seitz" <mseitz@meridian-data.com>
Date: 1999/05/28 Raw View
Siemel Naran <sbnaran@bardeen.ceg.uiuc.edu> wrote in message
news:slrn7kqvhd.jdf.sbnaran@bardeen.ceg.uiuc.edu...
> The function tmpfile() is neither part of C nor C++.
A follow-up to my follow-up:
P. J. Plauger's book THE STANDARD C LIBRARY says that tmpfile() is defined
in section 7.9.4.3 of the C Standard.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Darin Adler" <darin@bentspoon.com>
Date: 1999/05/28 Raw View
In article <slrn7kqvhd.jdf.sbnaran@bardeen.ceg.uiuc.edu>,
sbnaran@bardeen.ceg.uiuc.edu (Siemel Naran) wrote:
> The function tmpfile() is neither part of C nor C++.
It's described in section 4.9.4.3 "The tmpfile Function" of the ANSI C
standard, and in mentioned in section 27.8.2 "C Library Files" of the ISO
C++ standard.
tmpnam() is also standard, and I believe it could be used to create a name
for a file to be created with fstream.
-- Darin
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Ross Smith <ross.s@ihug.co.nz>
Date: 1999/05/28 Raw View
Siemel Naran wrote:
>
> On 27 May 1999 16:22:04 GMT, Michele Paoli <zizzi@cribecu.sns.it> wrote:
> >
> >For C-code I use tmpfile() C-function to create temporary files.
> >What can i do with fstream?
>
> The function tmpfile() is neither part of C nor C++. But it appears
> to be standard on UNIX and Linux. Look the man page of tmpfile (by
> typing "man tmpfile") and you'll see hints about other functions:
> mktemp, mkstemp, tmpnam, tempnam.
tmpfile() and tmpnam() are required by the C, and by inclusion C++,
standards.
To answer Michele's original question: Use tmpnam() to get a file name,
then use that to open an iostream. After you're done, close the stream
and remove() the file. Put all this in a class so it happens
automatically.
--
Ross Smith <ross.s@ihug.co.nz> The Internet Group, Auckland, New Zealand
========================================================================
I know you're desperate, I really sympathise
I see the morbid horror flicker in your eyes
But rest assured I'm gonna help to ease your pain
I'm gonna put a thousand tiny implants in your brain [Motorhead]
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: jcoffin@taeus.com (Jerry Coffin)
Date: 1999/05/28 Raw View
In article <374D1F91.ACDEEDD2@cribecu.sns.it>, zizzi@cribecu.sns.it
says...
[ ... ]
> For C-code I use tmpfile() C-function to create temporary files.
> What can i do with fstream?
You can create a name with tmpnam, then create and destroy the file
yourself. Just for fun, I wrote up a class named tmpstream that does
approximately the same sort of thing as tmpfile. It's main difference
is that a tmpstream object deletes the temporary files when it goes
out of scope instead of when the program closes. If you want the same
behavior as tmpfile, you simply use a tmpstream at file scope.
If you want a copy, feel free to send email, but it's probably not
particularly topical here.
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]