O_CREAT |?O_EXCL with ofstream

Hervé Eychenne rv@eychenne.org
Sun Jun 27 10:44:00 GMT 2004


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/



More information about the Libstdc++ mailing list