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:
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
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.
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