Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Closing All Files Opened with FOpen
Message
From
21/11/2001 21:11:28
 
 
To
21/11/2001 11:34:27
Jay Johengen
Altamahaw-Ossipee, North Carolina, United States
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00584535
Message ID:
00584781
Views:
22
This message has been marked as the solution to the initial question of the thread.
In some cases you can use an alternative approach that ensures there are no stray open files left but avoids "CLOSE ALL" type sledge-hammers:
local f, oGuard
* ...
f = fopen("foo")
oGuard = createobject([CFChanGuard], m.f)
* from here on you can RETURN anytime if you feel like it,
* without worrying about closing the file channel
* ...
and elsewhere
define class CFChanGuard as custom
   n_FChan = -1
   procedure init (nFChan)      
      this.n_FChan = m.nFChan
   procedure destroy
      if this.n_FChan <> -1
         fclose(this.n_FChan)
      endif
enddefine
Now, whenever the guard object vanishes - for example, if you leave the function scope via RETURN or because of an exception - the file channel will be closed. This is especially nice during testing because you can cancel the program anytime without worrying about stuff that might get left open. I use the same approach (which is a standard idiom in C++) for restoring various settings and releasing all kinds of resources, deleting temp files ... This stuff really grows on you once you get used to it, and it greatly helps unclutter program code.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform