如何关闭错误后保持打开状态的文件? [英] How can I close files that are left open after an error?

查看:170
本文介绍了如何关闭错误后保持打开状态的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  fid = fopen('fgfg.txt'); 

打开一个文件。

有时在设法关闭文件之前发生错误。

如果发生错误,怎样关闭文件?



  fclose all其次,你可以使用 try-catch 块并关闭你的文件句柄 

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

还有第三种方法,好多了。 Matlab现在是一个带有垃圾收集器的面向对象的语言。您可以定义一个自动处理其生命周期的包装器对象。

由于Matlab可以用这种方式调用对象方法:


<


方法(myObj)

您可以定义一个模拟所有相关文件命令的类,并封装生命周期。

  classdef safefopen<处理
属性(Access = private)
fid;
end

方法(Access = public)
函数this = safefopen(fileName,varargin)
this.fid = fopen(fileName,varargin {:});
结束

函数fwrite(this,varargin)
fwrite(this.fid,varargin {:});
结束

函数fprintf(this,varargin)
fprintf(this.fid,varargin {:});
end

函数delete(this)
fclose(this.fid);
end
end

end

强>删除运算符由Matlab自动调用。 (有更多的功能,你将需要包装,(fread,fseek等))。 现在你有了安全的句柄,可以自动关闭文件,不管你是否丢失了文件的范围或是发生了错误。



像这样使用它:

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

并且不需要关闭。

编辑:
我只是想包装 fclose()什么都不做。对于向后兼容性可能有用 - 对于使用文件ID的旧函数。


$ b 编辑(2):继@AndrewJanke好评,想通过在 fclose()

上抛出错误来改进删除方法
$ b $ $ $ $ $ $ $ $ $函数delete(this)
[msg,errorId] = fclose(this.fid);
if errorId〜= 0
throw(MException('safefopen:ErrorInIO',msg));
end
end


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?

解决方案

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

这篇关于如何关闭错误后保持打开状态的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆