fopen - open in exclusive read mode ?

Giganews Newsgroups
Subject: fopen - open in exclusive read mode ?
Posted by:  R.Wieser (addre…@not.available)
Date: Wed, 4 Nov 2020

Hello all,

I've got a small program which rewrites the contents of a textfile.  I'm
using "fopen" for both the source as well as the target file, reading lines
from the first and writing them to the second.

The problem is that when both have the same name the file is destroyed. :-((

To fix that I've been looking for an "exclusive read" open mode (so that
opening the targetfile would error-out).  The thing is that Windows doesn't
seem to offer it :

https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/fopen-wfopen

It does offer an "x" modifier, but that one only works on writing.

I've also found a reference to "flock", but that doesn't seem to be part of
the CRTDLL.DLL on XPsp3 .

tl;dr:
How do I use "fopen" (or a similar crtdll function) to get an exclusive read
lock ?

Regards,
Rudy Wieser

Replies