BASIC: How to check for an open file
Ron (2686) 63 posts |
Hello This thread does not own the current file handle. Ron |
Rick Murray (539) 13851 posts |
If you know the full canonicalised path 1, then…
Then when you have a valid file handle, call OS_Args 7 to get the pathname associated with a file handle. Then, it’s a simple comparison. ;-) PS: If you need it, OS_FSControl 37 will canonicalise a filename. 1 This means the full unambiguous path like SDFS::MyDisc.$.ThisDir.ThisFile instead of something like <My$Dir>.ThisFile. 2 This means making assumptions about valid file handles, assumptions which hold… for now… 3 Bit 11 set (or =2048) means the handle is unallocated (not in use). 4 The Stream Status Word doesn’t appear to be documented in the wiki?! |
Ron (2686) 63 posts |
Cheers Rick. but… I was hoping for something a little bit more Atomic. The time this takes, the file could have opened and closed several times from multiple requests so there’s still no guarantee of state by the time I try to open it, or even caching the file handle as this could have changed. Any way I can trap the Open request without the error and repeatedly wait until it opens ? Ron |
David J. Ruck (33) 1636 posts |
I just call OS_Args,7 with 255 to 1, and if V isn’t set the name is in the supplied buffer. |
Rick Murray (539) 13851 posts |
Mine is 3.38. http://www.riscos.info/downloads/stronghelp/manuals/
Are you worried about network files? Because it should be atomic if used on the local machine by software that scans the files without polling. As for networking, that’s a big complication, because if you can’t trust the file to be opened between checking the handles and opening the file, then that pretty much holds true whether you check one file or two hundred.
Yeah, don’t use OPENIN or OPENOUT. Directly call the XOS_Find SWI and just keep doing it until it returns a file handle and not an error. |
Alan Adams (2486) 1149 posts |
The simple way from BASIC is to handle the error. e.g.
This has the advantage that it opens the file if possible, and sets errno% if it isn’t, and returning 0 as the handle. Other methods run the risk of the file being found not open when tested, but open by something else when the subsequent attampt is made to open it. If you are using Reporter, you will see the error there, but the user won’t see an error. |