BBC BASIC
« Checking if a file is open »

Welcome Guest. Please Login or Register.
Apr 28th, 2017, 9:39pm


Cross-platform BBC BASIC (Win32, Linux x86, Android, Mac OS-X, Raspberry Pi)

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: Checking if a file is open  (Read 65 times)
Edja
New Member
Image


member is offline

Avatar




PM


Posts: 19
xx Checking if a file is open
« Thread started on: Feb 8th, 2017, 5:55pm »

I've been working on a program that uses the COMLIB library to interact with Excel sheets. One of the things I check in my code is if the Excel file is present in the right directory and if, by any chance, it isn't already open. So I've used the ideas from the WIKI at http://bb4w.wikispaces.com/Checking+whether+a+directory+exists
The following code results in a value 0 if the file doesn't exist and a non-zero value if the file exists. However, it doesn't discriminate if the file is open or not. The file handle is non-zero in both cases.
Code:
      ExcelFile$="C:\Users\Malbec\Documents\SomeFile.xls"
      ExcelFile%=OPENIN(ExcelFile$):CLOSE#ExcelFile%
      PRINT "File Handle =" ExcelFile% 

If the file is non-existant I generate an error message, but if the file exists I still need to know if it is open or not.
So I replaced OPENIN by OPENUP and my problem now seems solved:
Code:
      ExcelFile$="C:\Users\Malbec\Documents\SomeFile.xls"
      ExcelFile%=OPENUP(ExcelFile$):CLOSE#ExcelFile%
      PRINT "File Handle =" ExcelFile% 

If the file doesn't exist the behaviour is still the same (filehandle is 0). But now, if the file exists AND is open, the file handle is also zero. In this case I again generate an error message.
Only if the file exists AND is closed the file handle is non-zero. This completely covers my needs.

I know this is no rocket science, but since this different behaviour of OPENIN and OPENUP is not referred in the WIKI, I thought this idea could be interesting for others who run in to the same situation.

Edja
User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 437
xx Re: Checking if a file is open
« Reply #1 on: Feb 8th, 2017, 6:02pm »

on Feb 8th, 2017, 5:55pm, Edja wrote:
I know this is no rocket science, but since this different behaviour of OPENIN and OPENUP is not referred in the WIKI, I thought this idea could be interesting for others who run in to the same situation.

It should be safe in BB4W (at least, with files on the local disk), but you can't assume the behaviour will be the same in BBCSDL, in fact it almost certainly won't be. I don't have any control over the various sharing and access modes that are specified by SDL when it opens a file, and there are additional issues related to the different Operating Systems.

I don't suppose you care about the cross-platform compatibility of your technique, but I thought I should flag up a warning in case somebody does.

Richard.
User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Donate $6.99 for 50,000 Ad-Free Pageviews!


This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls