title: How to make sure that malloc/free fopen/fclose are matching?
body: <p>I think the following code is normal (and malloc/free is similar):</p>
  
  <pre><code>int foo(){
  
     FILE *fp = fopen("test.in", "r");
  
     int i;   
  
     for(i = 0; i &lt; NUM; i ++){
         if(Match(fp, i)){    
              fclose(fp);
              return i;
         }
     }
  
     fclose(fp);
     return 0;
  }
  </code></pre>
  
  <p>As we can see <code>fclose(fp)</code> appears twice in the code. It will appear more if there are other return statements in the function foo.  However, it is troublesome that I have to write <code>fclose(fp)</code> many times. One solution is just one return for one function. However, multiple returns is sometimes useful. Is there any other solution?</p>
  
  <p>PS: As I know, there is a macro(with-open-file) in lisp. </p>
  
  <pre><code>(with-open-file (stream-var open-argument*) 
      body-form*)
  </code></pre>
  
  <p>It could open and close file for us.</p>