Determining open files

I have a question about determining if a file is already open and/or in use. I'm writing software where one machine will be writing a file on a server and another will be reading that file. The file is small and can be written very quickly, but I'm sure I will run into the situation where the one machine will be trying to read the file while the other is trying to write the file. I can probably use a When Error statement to get one or the other machines to just wait, but is there a more elegant way to determine if a file is already open or in use?



re: more on open files

There doesn't seem to be any ASK statement that is appropriate here--any documented ones anyway. I have ERROR trapped in the following way:

LET writeflag = 0
WHEN error in
OPEN #3:name datadir$&"maindata",access outin,create newold,org record
WRITE #3:year
WRITE #3:supdem
WRITE #3:int((gh+waste+air)/3)
WRITE #3:fd
LET writeflag = 1
BOX KEEP 312,712,500,300 IN TEMP$
IF writeflag = 1 then EXIT DO

Where the box show's display an error message. I do the same thing in both programs (running on different computers). Let me expand here.....

I eventually want one computer running a main program that reads data from 5 different files, each of those being written by programs running on 5 other machines. The MAIN program will also write (as above) a file that is read periodically by the other 5 programs to keep them updated as to the progress. Basically the 5 separate machines/programs are control panels with a number of controls that can be changed on the fly. The main program reads the 5 control panel files--uses that data as input to the main program--runs and updates parameters (like the year) and then sends that information back to the 5 control panels through the file that is written in the code above. The main program reads these files every few seconds while the control panels rewrite their files whenever a change is made and read the file from the main program every few seconds.

All of this works--the files being kept on a server to which all machines have access.

The problem here is that when there is an error--everything hangs for several seconds. The error I have been able to produce seems to be the main program reading the control panel file while that panel program is re-writing the file. Anyway the control panel freezes for several seconds--then the error message is displayed on the main screen, then everything resets and works again. Unfortunately, I am only testing with one panel and the main program. With 5 panels running, I suspect there will be many more errors.

So...I'm still looking for a more elegant (and executionally quicker) way to determine if a file is in use. Any ideas?


open files


This is a common problem with networks. Multiple access for reading is permitted but simultaneous reading while writing causes an error.

There is a relatively simple way to work around the problem using the EXEC_rename sub routine in EXECLIB.TRC

Basically when any of your programs wishes to use a file it must first attempt to open the file as CREATE OLD. Surround this statement with an exception handler to keep the program running if the open statement fails. Put the whole block inside a loop so that the program keeps trying to open the file. On success, you exit the loop. At this point you use EXEC_rename to change the name of the file you want to read. Now you read or write to the file using its new name, knowing that none of the other programs can access the file because you have renamed it. When you have finished reading/writing you change the name back to its original name so that other programs can again access the file.

Big John

success with this

Not sure anyone else cares about this, but BigJohn's advice here need to do the Exec_Rename before opening the file (and now open with the new filename) and then to switch back, do another rename just after closing the file. It is also important (I think) to put the first Rename inside the ERROR trap since if another program has renamed the target file (in order to read or write to it) then the Exec_Rename call will not find the file and will return an error.


re:open files

Thanks BigJohn,

I had used the error trapping you suggest--but just basically on the opening of the file. I didn't know about the EXEC_rename command and will try that when I can get back into school to work on this--I need two computers and a network server to develop--but it looks like it should work.


Re: Determining open files ...

rtarara ... If memory serves me, I think you need to use an ASK statement, but I can't remember the Forum message where I saw that statement. I think "Big John" Arscott has the answer. Regards ... Tom M