Okay, I finally got a chance to test this under WinNT... and I got the same results I got under Win2K. With a file open for read-only access in Perl, notepad and gvim don't care in the least and will still open, edit, and save the file.

It is nice that several people have chimed in with how "this is just how Windows works", but I don't believe any of them. (: I've certainly seen cases where shared access was denied when I had hoped that it wouldn't be, but a quick test shows that this isn't such a case.

It is true that, unlike Unix, Windows gives you the option of requesting that no other program do certain things to the file while you are using it and of having that request enforced by the operating system (not requiring all tasks dealing with the file to cooperate nor requiring special tagging of the file to get this).

It is also true that many Windows programs, especially those not written in C, chose to request no sharing when they open files (or at least less sharing than you sometimes would like).

The details are that you can request three types of sharing: read, write, and delete. ("Delete" sharing doesn't apply prior to WinNT.) You specify zero or more of these types of sharing be allowed whenever you open a file in Windows. When using C code, the default is to request only "read" and "write" sharing.

If you don't request "read" sharing, then you won't be able to open the file if anyone else has the file open for reading and once you have the file open, nobody else will be allowed to open it for reading. "write" sharing is exactly the same except you replace "reading" with "writing".

See Win32API::File for some more information on this and how to use Perl to play with opening files specifying different types of sharing.

        - tye (but my friends call me "Tye")

In reply to (tye)Re: Unable to write to a file opened in read only mode by tye
in thread Unable to write to a file opened in read only mode by cgrinder

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.