www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Reading file contents when file has changed

reply "OlaOst" <olaa81 gmail.com> writes:
I'm working on a program (using dmd 2.059 under windows) that 
automatically reloads the contents of a file if it has changed, 
by checking the last modified timestamp on the file every 0.2 
seconds, then using the readText function in std.file to read in 
the file contents.

It works pretty well, but every once in a while I get a "The 
process cannot access the file because it is in use by another 
process" error message. I guess this happens when my D program 
tries to read the file before the text editor manages to close 
its handle on the file.

This would be fair enough if I was writing to the file in my D 
program, but I only want to read from it. Is this a bug or is 
there a better way to get file contents in D?
Jul 13 2012
parent reply Kevin Cox <kevincox.ca gmail.com> writes:
--e89a8f642d949bb9db04c4bc2e70
Content-Type: text/plain; charset=UTF-8

On Jul 13, 2012 4:40 PM, "OlaOst" <olaa81 gmail.com> wrote:
 I'm working on a program (using dmd 2.059 under windows) that

the last modified timestamp on the file every 0.2 seconds, then using the readText function in std.file to read in the file contents.
 It works pretty well, but every once in a while I get a "The process

message. I guess this happens when my D program tries to read the file before the text editor manages to close its handle on the file.
 This would be fair enough if I was writing to the file in my D program,

get file contents in D? While the text editor is writing the file reading it is a silly thing to do (no one knows what's there). I'm not familiar with windows but locking under Linux works as so. Any number or processes can have a read lock but if there is a write lock only the single process can access that file. Also isn't there something like inotify on windows? Polling sounds like the wrong solution. --e89a8f642d949bb9db04c4bc2e70 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable <p><br> On Jul 13, 2012 4:40 PM, &quot;OlaOst&quot; &lt;<a href=3D"mailto:olaa81 gm= ail.com">olaa81 gmail.com</a>&gt; wrote:<br> &gt;<br> &gt; I&#39;m working on a program (using dmd 2.059 under windows) that auto= matically reloads the contents of a file if it has changed, by checking the= last modified timestamp on the file every 0.2 seconds, then using the read= Text function in std.file to read in the file contents.<br> &gt;<br> &gt; It works pretty well, but every once in a while I get a &quot;The proc= ess cannot access the file because it is in use by another process&quot; er= ror message. I guess this happens when my D program tries to read the file = before the text editor manages to close its handle on the file.<br> &gt;<br> &gt; This would be fair enough if I was writing to the file in my D program= , but I only want to read from it. Is this a bug or is there a better way t= o get file contents in D?</p> <p>While the text editor is writing the file reading it is a silly thing to= do (no one knows what&#39;s there).=C2=A0 I&#39;m not familiar with window= s but locking under Linux works as so.=C2=A0 Any number or processes can ha= ve a read lock but if there is a write lock only the single process can acc= ess that file.</p> <p>Also isn&#39;t there something like inotify on windows?=C2=A0 Polling so= unds like the wrong solution.</p> --e89a8f642d949bb9db04c4bc2e70--
Jul 13 2012
parent Simon <s.d.hammett gmail.com> writes:
On 13/07/2012 21:47, Kevin Cox wrote:
 On Jul 13, 2012 4:40 PM, "OlaOst" <olaa81 gmail.com
 <mailto:olaa81 gmail.com>> wrote:
  >
  > I'm working on a program (using dmd 2.059 under windows) that
 automatically reloads the contents of a file if it has changed, by
 checking the last modified timestamp on the file every 0.2 seconds, then
 using the readText function in std.file to read in the file contents.
  >
  > It works pretty well, but every once in a while I get a "The process
 cannot access the file because it is in use by another process" error
 message. I guess this happens when my D program tries to read the file
 before the text editor manages to close its handle on the file.
  >
  > This would be fair enough if I was writing to the file in my D
 program, but I only want to read from it. Is this a bug or is there a
 better way to get file contents in D?

 While the text editor is writing the file reading it is a silly thing to
 do (no one knows what's there).  I'm not familiar with windows but
 locking under Linux works as so.  Any number or processes can have a
 read lock but if there is a write lock only the single process can
 access that file.

 Also isn't there something like inotify on windows?  Polling sounds like
 the wrong solution.

Yes: http://msdn.microsoft.com/en-us/library/windows/desktop/aa365261%28v=vs.85%29.aspx It's not straight forward to use though and I doubt that the necessary function definitions are provided by the default dmd windows imports. -- My enormous talent is exceeded only by my outrageous laziness. http://www.ssTk.co.uk
Jul 13 2012