TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: Pascal Schmidt
from: John H. Guillory
date: 2004-05-02 16:32:58
subject: Merits

Re: Merits
  By: Pascal Schmidt to John H. Guillory on Sun May 02 2004 05:34 pm

 >  JHG> "break" out of a loop, or "GOTO" a label, etc.
 > You can, no doubt on that. However, sometimes a break or goto can be much
 > clearer in the end.
 > 
 > Consider:
 > 
 >     FILE *fp;
 >     int res;
 > 
 >     fp = fopen("foo", "w");
 >     res = write(fp, ...); /* some data */
 >     if (res = -1) {
 >         fclose(fp);
 >         return -1;
 >     }
 >     res = write(fp, ...); /* some other data */
 >     fclose(fp);
 >     return res;
FILE *fp;
int res;

    fp = fopen("foo","w");
    if (fp != NULL) {
       res = write(fp, ...);
       if (res != -1) {
         res = write(fp, ...);
       }
     }
     fclose(fp);

is more along the lines of how to do it, though in the above, it looks
more like your wanting to do C++ try/catch statements... The above lines
of code can be made easier to read via making macros out of them, and yet
they still maintain their simplicity.
      
 > and:
 > 
 >     FILE *fp;
 >     int res;
 > 
 >     fp = fopen("foo", "w");
 >     res = write(fp, ...);
 >     if (res == -1)
 >         goto out;
 >     res = write(fp, ...);
 > out:
 >     fclose(fp);
 >     return res;
 > 
 > The latter keeps the cleanup code (in this case only one fclose, but there
 > could be several lines of cleanup code) in one place and not duplicated at e
 > possible exit point.

And what's wrong with:

void CleanUp(FILE *fp) {
     fclose(fp);
}

and adding 1 line that reads CleanUp(fp);  after closing if?

--- SBBSecho 2.00-Win32
* Origin: MLC-Group BBS -- mlc-group.dyndns.org (1:396/60)
SEEN-BY: 633/267 270
@PATH: 396/60 45 106/2000 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.