Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I close files that are left open after an error?

Tags:

file-io

matlab

I am using

fid = fopen('fgfg.txt'); 

to open a file.

Sometimes an error occurs before I manage to close the file. I can't do anything with that file until I close Matlab.

How can I close a file if an error occurs?

like image 825
Lee Avatar asked Jan 13 '12 08:01

Lee


People also ask

How do I force close a file?

To quickly force quit on Windows, use the keyboard shortcut Alt + F4. Make sure the app or program window is open when you click Alt + F4. You can also force quit on Windows by using the Task Manager or Command Prompt.

How do you close a file that is open in system?

By right click on the wanted file, choose an option, “Close open file”, and that will close it.

What happens if file is not closed?

If you write to a file without closing, the data won't make it to the target file. But after some surfing I got to know that Python automatically closes a file when the reference object of a file is reassigned to another file. It is a good practice to use the close() method to close a file.

Are file descriptors closed automatically?

Files are automatically closed, but it's a good practice. Show activity on this post.


1 Answers

First of all, you can use the command

fclose all 

Secondly, you can use try-catch blocks and close your file handles

 try      f = fopen('myfile.txt','r')      % do something      fclose(f);  catch me      fclose(f);      rethrow(me);  end 

There is a third approach, which is much better. Matlab is now an object-oriented language with garbage collector. You can define a wrapper object that will take care of its lifecycle automatically.

Since it is possible in Matlab to call object methods both in this way:

myObj.method()

and in that way:

method(myObj)

You can define a class that mimics all of the relevant file command, and encapsulates the lifecycle.

classdef safefopen < handle     properties(Access=private)         fid;     end      methods(Access=public)         function this = safefopen(fileName,varargin)                         this.fid = fopen(fileName,varargin{:});         end          function fwrite(this,varargin)             fwrite(this.fid,varargin{:});         end          function fprintf(this,varargin)             fprintf(this.fid,varargin{:});         end          function delete(this)             fclose(this.fid);         end     end  end 

The delete operator is called automatically by Matlab. (There are more functions that you will need to wrap, (fread, fseek, etc..)).

So now you have safe handles that automatically close the file whether you lost scope of it or an error happened.

Use it like this:

f = safefopen('myFile.txt','wt') fprintf(f,'Hello world!'); 

And no need to close.

Edit: I just thought about wrapping fclose() to do nothing. It might be useful for backward compatibility - for old functions that use file ids.

Edit(2): Following @AndrewJanke good comment, I would like to improve the delete method by throwing errors on fclose()

    function delete(this)                   [msg,errorId] = fclose(this.fid);         if errorId~=0             throw(MException('safefopen:ErrorInIO',msg));         end     end 
like image 194
Andrey Rubshtein Avatar answered Oct 02 '22 08:10

Andrey Rubshtein