Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Closing All Files Opened with FOpen
Message
De
21/11/2001 21:11:28
 
 
À
21/11/2001 11:34:27
Jay Johengen
Altamahaw-Ossipee, Caroline du Nord, États-Unis
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
00584535
Message ID:
00584781
Vues:
27
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.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform