www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - FileMode.Out

reply "Chris Miller" <chris dprogramming.com> writes:
I open a File with FileMode.Out and I can't read the file from other  
programs while the file is open. Why is the file sharing set this way? It  
has been annoying me all the way from my early days of D.

I often open a file at program startup and write log information to it;  
sometimes the program runs 24 hours a day. What good is a log file if I  
can never read it?

Please set file sharing to always share read access. It should be 2 simple  
fixes to stream.d in function parseMode.

Also, there could be a way to extend File to allow specifying file  
sharing. Since not every environment offers this functionality, it could  
just be a request, and there could also be another function to call that  
returns true if sharing is definitely available, or false if it is not or  
cannot be determined.

A lot of times I just go back to C FILE*s because D's streams just feel  
icky.


Oh yeah, I forgot, opening for write causes sharing write access. This is  
really bizarre. It should always share read and should never share write  
(unless there's a way to change it per File).


I'm going to be pretty mad if this isn't fixed for 1.0 - especially the  
sharing write.
Jul 23 2006
parent "Chris Miller" <chris dprogramming.com> writes:
On Mon, 24 Jul 2006 02:17:07 -0400, Chris Miller <chris dprogramming.com=
  =

wrote:
 I open a File with FileMode.Out and I can't read the file from other  =

 programs while the file is open. Why is the file sharing set this way?=

 It has been annoying me all the way from my early days of D.

 I often open a file at program startup and write log information to it=

 sometimes the program runs 24 hours a day. What good is a log file if =

 can never read it?

 Please set file sharing to always share read access. It should be 2  =

 simple fixes to stream.d in function parseMode.

 Also, there could be a way to extend File to allow specifying file  =

 sharing. Since not every environment offers this functionality, it cou=

 just be a request, and there could also be another function to call th=

 returns true if sharing is definitely available, or false if it is not=

 or cannot be determined.

 A lot of times I just go back to C FILE*s because D's streams just fee=

 icky.


 Oh yeah, I forgot, opening for write causes sharing write access. This=

 is really bizarre. It should always share read and should never share =

 write (unless there's a way to change it per File).


 I'm going to be pretty mad if this isn't fixed for 1.0 - especially th=

 sharing write.

Sorry, forget the last part, it doesn't share write access. Here's an updated parseMode function to always only give read sharing: private void parseMode(int mode, out int access, out int share, out int createMode) { version (Win32) { share |=3D FILE_SHARE_READ; if (mode & FileMode.In) { access |=3D GENERIC_READ; createMode =3D OPEN_EXISTING; } if (mode & FileMode.Out) { access |=3D GENERIC_WRITE; createMode =3D OPEN_ALWAYS; // will create if not present } if ((mode & FileMode.OutNew) =3D=3D FileMode.OutNew) { createMode =3D CREATE_ALWAYS; // resets file } } version (linux) { share =3D 0444; if (mode & FileMode.In) { access =3D O_RDONLY; } if (mode & FileMode.Out) { createMode =3D O_CREAT; // will create if not present access =3D O_WRONLY; } if (access =3D=3D (O_WRONLY | O_RDONLY)) { access =3D O_RDWR; } if ((mode & FileMode.OutNew) =3D=3D FileMode.OutNew) { access |=3D O_TRUNC; // resets file } } } Another concern, when opening for write and not specifying read, should = it = imply also opening for read?
Jul 23 2006