[MLton] File open flags on Windows (was: experimental ...)

Anoq of the Sun anoq@HardcoreProcessing.com
Wed, 19 Nov 2003 22:16:48 +0200


Stephen Weeks wrote:
> Cygwin is unavailable because of the problem that Bin Li and a couple
> of others with building the world.  I've done some investigating and
> found that the call to open to create the world file is mysteriously
> failing, despite having perfectly valid arguments.

I am doing Windows programming these days and happen to have had
problems with exactly this on Windows the last couple of weeks,
so here is a summary of what to do and what to remember on Windows:

   Basically there are some flags you can set on Windows to the
   open command. These are:

1) The Create flag. If you want to open a file which does not exist
   (i.e. with the intent of creating the file) and this flag is
   not set - then the open command will fail. So to create a new
   file you must set this flag.
2) The NoTruncate flag. If you open a file and you do not wish to
   truncate the file (e.g. if you want to append to the end of the file)
   you must set this flag.
3) The Read and Write flags. These must be set according to what you
   want to do.
4) The Binary and Text flags. These flags determine if you read or
   write the file binarily (i.e. the actual bytes in the file) or
   if you read or write the file as a text file. If you use the
   text mode you should be prepared for the file reading and writing
   doing some conversion regarding <LF> vs. <CR><LF>. You should also
   be aware that character 26 (I think - or maybe it's 24 or something?)
   means end of file. If you encounter such a byte when reading you won't
   be able to read any more. If you write it to a file I believe that
   the file might also close for further writing.
   To make the whole thing perfect - I think the default flag if
   you don't set any of these is to use the Text mode with all the
   wierd conversion and EOF stuff :)
5) There are also some flags regarding permissions - such as ShareDenyWrite,
   ShareDenyRead, ShareRead, ShareWrite etc. These should only be relevant
   to set if a file is opened from multiple places at the same time.
   You can also expect these flags to be counter intuitive and mean
   the opposite of what you think as a first thought :)
   (e.g. does DenyWrite apply to _you_ or _others_ who open the file?)

I think there are even some combinations of flags which Windows
doesn't like (especially regarding the permissions) :)

If you are using the fopen command, it uses a string with these
flags. And remember that "rb" is read binary and "wb" is write binary
(so just using "r" or "w" will give the problems for binary data
described above).

Does this bring some determinism to solving the problem? :)


Cheers
-- 
http://www.HardcoreProcessing.com