Detecting if a file is open

John W. Eaton jwe at bevo.che.wisc.edu
Mon Jun 2 08:24:02 CDT 2008


On  2-Jun-2008, gOS wrote:

| Let me give you a specific example:
| 
| function = foo(bar.txt)
| 
| try
|    fid = fopen(bar.txt)
|    if(fid < 0)
|       warning('file never opened)
|       return;
|    end
| 
|    % do a bunch of stuff with the file
| 
|    % ! an unforeseen error occurs here %
| 
|    % do more stuff with the file
| 
|    fclose(fid)
| 
| catch
| 
|   % detect whether the file is still open (see original post)
| 
|   if ( fileIsOpen )
|      fclose(fid)
|   end
| end

You want unwind_protect, not try/catch.

jwe


More information about the Help-octave mailing list