File needs to be closed on Windows but not on Posix, bug?

Joakim via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jul 7 10:09:22 PDT 2014


On Monday, 7 July 2014 at 12:00:48 UTC, Regan Heath wrote:
> On Mon, 07 Jul 2014 12:17:34 +0100, Joakim 
> <dlang at joakim.airpost.net> wrote:
>
>> On Monday, 7 July 2014 at 10:19:01 UTC, Kagamin wrote:
>>> See if stdio allows you to specify delete sharing when 
>>> opening the file.
>>
>> I don't know what delete sharing is exactly, but the File 
>> constructor simply calls fopen and I don't see any option for 
>> the Windows fopen that seems to do it:
>>
>> http://msdn.microsoft.com/en-us/library/yeby3zcb.aspx
>
> The fopen variant that allows you to specify sharing is:
> http://msdn.microsoft.com/en-us/library/8f30b0db.aspx
>
> But it does not mention delete sharing there.
>
> CreateFile allows sharing to be specified for delete however:
> http://msdn.microsoft.com/en-gb/library/windows/desktop/aa363858(v=vs.85).aspx
>
> So... you could:
>  - Call CreateFile giving you a "handle"
>  - Call _open_osfhandle to get a "file descriptor"
>  - Call _fdopen on the file descriptor to get a FILE* for it
>
> But!  I agree with Adam, leave it as a thin wrapper.  Being a 
> windows programmer by trade I would expect the remove to fail, 
> I would not expect all my files to be opened with delete 
> sharing enabled by default.

Thanks for all the info.  I'm looking at it from the point of 
view of a beginner or someone who just writes a quick D script 
that does something like this.  They may find it confusing that 
it works on linux but doesn't work on Windows, especially given 
the confusing error message.

But it is good practice to close a file before deleting and if D 
prefers to have thin wrappers around OS APIs, that implies less 
hand-holding like this, so maybe we just tell such people to 
close the file first.


More information about the Digitalmars-d-learn mailing list