Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2021-01

Avoiding File conflicts

4 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

James Richters

I am checking for the existence of a file in my main program loop that runs about 4 times a second waiting for keystrokes etc... If I see the file exists, I just want to open the file, read it into a buffer and process it.

This works most of the time, but every once in a while I get EInOutError: Access denied with a return code of 217

I suspect that I happen to see the file is there and try to read it before the program that created the file is done writing it. What is the proper way to detect the file is in use so I don't bother trying to open it until it's done being written?

I'm on a windows system if that makes any difference and this is a console program.

James

Reply by email

Bart
to James Richters
AM James Richters wrote
I suspect that I happen to see the file is there and try to read it before the program that created the file is done writing it. What is the proper way to detect the file is in use so I don't bother trying to open it until it's done being written?

Simple approach?
Use filemode = fmOpenRead and use a try..except block to open the file? Opening will fail if the file is locked by another process (triggering the exception).

Bart

Reply by email

Unknown author
to Bart
Bart wrote 10 lines
On Mon, Jan 4, 2021 at 3:19 AM James Richters via fpc-pascal wrote: > I suspect that I happen to see the file is there and try to read it before the program that created the file is done writing it. > What is the proper way to detect the file is in use so I don't bother trying to open it until it's done being written? Simple approach? Use filemode = fmOpenRead and use a try..except block to open the file? Opening will fail if the file is locked by another process (triggering the exception).

provided the other program opens the file for creation properly (eg: blahDenyAll), right?

NOTE: No off-list assistance is given without prior approval. *Please keep mailing list traffic on the list where it belongs!*

Reply by email

Bart
to Unknown author
PM wkitty42--- wrote
provided the other program opens the file for creation properly (eg: blahDenyAll), right?

Maybe use fmShareExclusive when opening the file.
That name suggest you can only open if you are the only one?

Bart

Reply by email