Detecting if a file is open

Sergei Steshenko sergstesh at yahoo.com
Fri May 30 17:39:06 CDT 2008


--- gOS <bkirklin at quantapoint.com> wrote:

> 
> I'm wondering if there is a better way to do this.
> 
> fid = fopen('testfile.txt','w')
> 
> if(exist('fid'))
>    if(isequal(ferror(fid),''))
>      fclose(fid);
>    end
> end
> 
> essenially, I'm assuming that if ferror is an empty string, then the file
> needs closed. I'm using try catch error handling, and just want to make sure
> everything is cleaned up properly before I exit the specific function. This
> is useful when you want to work with a file without having to completely
> shut down octave and restart it.
> 
> Is there a method that is more often used to tell if a file is open? Perhaps
> more reliably? I just played around until I found this.
> -- 
> View this message in context:
> http://www.nabble.com/Detecting-if-a-file-is-open-tp17566839p17566839.html
> Sent from the Octave - General mailing list archive at Nabble.com.
> 
> _______________________________________________
> Help-octave mailing list
> Help-octave at octave.org
> https://www.cae.wisc.edu/mailman/listinfo/help-octave
> 

I do not understand what problem you are trying to deal with - I see at least two
issues:

1) fid <-> file relationship;
2) fid value for open files.

I solve 1) using a trivial naming connection, for example:

my_file = "my_file.txt";
my_file_fid = fopen(my_file, "w");

2) is trivial - open files have positive fids.

You may also organize fids as a hash table ("struct" in 'octave' terms) and delete fid from
the table as soon as file is closed, so the list of keys will represent the list of currently
open files.

Regards,
  Sergei.

Applications From Scratch: http://appsfromscratch.berlios.de/


      


More information about the Help-octave mailing list