"Hrvoje Niksic" wrote:

Is there a way to get the functionality of open(..., O_CREAT|O_EXCL)
under Windows?  For those who don't know, O_EXCL opens the file
"exclusively", guaranteeing that the file we're opening will not be
overwritten.  (Note that it's not enough to check that the file
doesn't exist before opening it; it can spring into existence between
the check and the open.)

This works with MingW and MSVC. Watcom acts a bit odd if you use fdopen (fd, "w+") afterwards. A snippet from my contribution to libnet (works across processes of course):

#include <fcntl.h>
#include <share.h>
#include <io.h>

int open_flags = O_WRONLY | O_CREAT | O_TRUNC;
/* possibly drop the O_WRONLY for r/w */

int fd = sopen (file_name, open_flags | O_BINARY | _O_SEQUENTIAL,
                SH_DENYWR, S_IREAD | S_IWRITE);

_O_SEQUENTIAL is just to tell the cache manager to stop wasting
memory.


--gv

Reply via email to