help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Detecting if a file is open


From: Sergei Steshenko
Subject: Re: Detecting if a file is open
Date: Fri, 30 May 2008 15:39:06 -0700 (PDT)

--- gOS <address@hidden> 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
> address@hidden
> 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/


      


reply via email to

[Prev in Thread] Current Thread [Next in Thread]