• Re: fopen - open in exclusive read mode ?

    From holyghost@21:1/5 to All on Fri Sep 24 05:33:16 2021
    On 11/6/2020 2:33 PM, JJ wrote:
    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.)

    holly

    --
    (__)
    oo )
    |_/\

    My Software & Art company (donations are welcome) : https://ko-fi.com/brandywine9

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From R.Wieser@21:1/5 to All on Fri Sep 24 11:36:24 2021
    holyghost,

    AFAIK you don't need a read lock for file access (see a database course.)

    :-) A database locks the record when writing to it, so that when reading
    there is no way that you will get half the old and half the new record.

    But I suggest you read my problem specification first. Its not about the
    read and write actions themselves.

    Regards,
    Rudy Wieser

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)