On Wed, 4 Nov 2020 13:53:28 +0100, R.Wieser wrote:
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
Not possible using `fopen()`. Use `fsopen()` instead. `fopen()` is simply
not share aware.
`fopen()` uses `_SH_DENYNO` sharing flag no matter what. That flag
translates to Windows' `FILE_SHARE_READ` and `FILE_SHARE_WRITE`.
AFAIK you don't need a read lock for file access (see a database course.)
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 379 |
Nodes: | 16 (2 / 14) |
Uptime: | 42:45:07 |
Calls: | 8,141 |
Calls today: | 4 |
Files: | 13,085 |
Messages: | 5,857,851 |