如何判断文件是否已关闭。 [英] How to tell a file is been close.

查看:114
本文介绍了如何判断文件是否已关闭。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

FILE * fp;


fp = fopen(...)

fclose(fp)。

怎么做我知道fp是否已经fclose。

我知道在fclose之后可以将fp设置为NULL,然后检查fp'的值为

解决问题。

但是,如果还有其他方法吗?

解决方案

empriser说:


FILE * fp;


fp = fopen(...)

fclose(fp)。



fclose可能会失败。检查返回结果。 (在这里,我和你说的一样多,我自己也是这样说的!)


我怎么知道是否fp已经fclose。

我知道在fclose之后可以将fp设置为NULL,然后检查fp'的值为

解决问题。



对。当你调用fclose时,fp的值变得不确定,

这意味着你不再允许你将这个值用于任何

目的。所以你能做的最好的事情就是给fp一个新值,你可以/ b $ b / can / use,而NULL是明显的候选者。


但是,如果有其他方法?



No.


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名中, - www。


empriser写道:


FILE * fp;


fp = fopen(...)

fclose(fp)。


如何知道fp是否已经fclose。



你必须记住你已经关闭了它。 (或者,最好,

安排您不必关心。)


我知道fclose后可以将fp设置为NULL它,然后检查fp'的值为

解决问题。



如果有其他副本的`fp`s值,这没有用。


一个有用的技巧是安排您的文件处理代码如下:


{FILE * fp = fopenSuitably()

; doThingsWithFile(fp)

; fclose(fp)

; }


(别介意我的布局,今天我正在进行实验。)


(插入检查`fclose`努力品尝。)


现在,这段代码太短了很容易检查

你从来没有使用`fp`已经关闭。 (假设

`doThingsWithFile`并不会故意将`fp`复制到其他地方。)


有时你可以安排`doThingsWithFile`是

函数指针。 (如果它需要额外的状态,也可以传递它。)


如果你的大部分文件处理都可以这样做,你只需要

到对文件的其他用途深表怀疑。


另一个技巧是有一个结构


struct fileAndBoolean

{YourBooleanType已关闭

; FILE * fp

; } $ / $

在`struct fileAndBoolean *`上编写文件访问方法。让他们

检查`closed`是否属实。在'close()`上设置`closed`。


这些技巧是否对你有价值取决于你的背景。


-

这只是我们见过的开始 - 斗兽场,/明天的蓝调/

Hewlett-Packard Limited注册办事处:Cain Road,Bracknell,

注册号:690597 England Berks RG12 1HN


empriser写道:


FILE * fp;


fp = fopen(...)

fclose(fp)。


如何知道fp是否已经fclose。



fclose()在成功时返回0。


我知道在fclose之后可以将fp设置为NULL ,然后检查fp'的值为

解决问题。



有什么问题?


-

Tor


FILE *fp;

fp = fopen( ... )
fclose( fp ).
How do I know if fp has been fclose.
I know can set fp to NULL after fclose it, then check fp''s value to
solve the problem.
But, if there have other methods?

解决方案

empriser said:

FILE *fp;

fp = fopen( ... )
fclose( fp ).

fclose can fail. Check the return result. (Here, I''m speaking as much to
myself as to you!)

How do I know if fp has been fclose.
I know can set fp to NULL after fclose it, then check fp''s value to
solve the problem.

Right. When you call fclose, the value of fp becomes indeterminate,
which means that you are no longer allowed to use that value for any
purpose. So the best thing you can do is give fp a new value that you
/can/ use, and NULL is the obvious candidate.

But, if there have other methods?

No.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.


empriser wrote:

FILE *fp;

fp = fopen( ... )
fclose( fp ).
How do I know if fp has been fclose.

You have to remember that you''ve closed it. (Or, preferably,
arrange that you don''t have to care.)

I know can set fp to NULL after fclose it, then check fp''s value to
solve the problem.

That doesn''t help if there are other copies of `fp`s value.

A useful trick is to arrange that your file-handling code looks like:

{ FILE* fp = fopenSuitably()
; doThingsWithFile( fp )
; fclose( fp )
; }

(Don''t mind my layout, I''m being experimental today.)

(Insert check on `fclose` working to taste.)

Now, this piece of code is so short it''s easy to check that
you never use `fp` after its been closed. (Assuming that
`doThingsWithFile` doesn''t wilfully copy `fp` elsewhere.)

And sometimes you can arrange that `doThingsWithFile` is a
function pointer. (If it needs extra state, pass that in too.)

If most of your file-handling can be done that way, you only need
to be deeply suspicious of other uses of files.

Another trick is to have a struct

struct fileAndBoolean
{ YourBooleanType closed
; FILE* fp
; }

Write file-accessing methods on `struct fileAndBoolean*`. Have them
check whether `closed` is true. Set `closed` on `close()`.

Whether these tricks are of value to you depends on your context.

--
"It''s just the beginning we''ve seen" - Colosseum, /Tomorrow''s Blues/

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN


empriser wrote:

FILE *fp;

fp = fopen( ... )
fclose( fp ).
How do I know if fp has been fclose.

fclose() returns 0 on success.

I know can set fp to NULL after fclose it, then check fp''s value to
solve the problem.

what is the problem?

--
Tor


这篇关于如何判断文件是否已关闭。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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