This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: O_CREAT |?O_EXCL with ofstream
- From: Hervé Eychenne <rv at eychenne dot org>
- To: Jonathan Wakely <cow at compsoc dot man dot ac dot uk>
- Cc: libstdc++ at gcc dot gnu dot org
- Date: Sat, 26 Jun 2004 16:08:04 +0200
- Subject: Re: O_CREAT |?O_EXCL with ofstream
- References: <20040528160715.GB1935@eychenne.org> <20040528161355.GA3079@compsoc.man.ac.uk>
On Fri, May 28, 2004 at 05:13:55PM +0100, Jonathan Wakely wrote:
> > I used to use:
> > int fd = open("file", O_CREAT |?O_EXCL);
> > ofstream os(fd);
> > but ofstream::ofstream(int fd) has also been deprecated (sigh), so
> > I see no way anymore to atomically ensure that a file was not already
> > existing when creating a new one.
> > I've searched everywhere on the Web, and I could only find a
> > reference of a ios_base::noreplace which does not seem to exist.
> >
> > So how do I get an ofstream while ensuring atomicity? (O_CREAT |?O_EXCL behaviour)
> > Any clue?
> Which compiler version are you using?
g++-3.3.4
> Have you looked at the <ext/stdio_filebuf.h> header?
> See http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#11
Maybe I'm a little short minded, but I do not see what to do
concretely...
All I want to do is a simple and portable way to do something like:
int fd = open("file", O_WRONLY | O_CREAT | O_EXCL);
myclass obj(fd, ios::out);
obj << "foo";
That should be so simple to do... but the best I could achieve was:
int fd = open("file", O_WRONLY | O_CREAT | O_EXCL);
__gnu_cxx::stdio_filebuf<char> obj(fd, ios::out, false, BUFSIZ);
obj << "foo";
and obj << "foo" fails...
Please note that I really want to be able to use operator<<.
Herve
--
_
(°= Hervé Eychenne
//) Homepage: http://www.eychenne.org/
v_/_ WallFire project: http://www.wallfire.org/