Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How to tell a file is been close.

Reply
Thread Tools

How to tell a file is been close.

 
 
empriser
Guest
Posts: n/a
 
      05-17-2007
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?

 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      05-17-2007
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.
 
Reply With Quote
 
 
 
 
Chris Dollin
Guest
Posts: n/a
 
      05-17-2007
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

 
Reply With Quote
 
Tor Rustad
Guest
Posts: n/a
 
      05-17-2007
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
 
Reply With Quote
 
christian.bau
Guest
Posts: n/a
 
      05-17-2007
On May 17, 12:11 pm, Richard Heathfield <(E-Mail Removed)> wrote:

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


The question is: If it fails, what are you supposed to do? fclose can
obviously fail if I make some programming error, like fclose (fp);
fclose (fp); where the second call is quite likely to fail. But apart
from that, what can you do?

 
Reply With Quote
 
quarkLore
Guest
Posts: n/a
 
      05-17-2007
On May 18, 1:14 am, "christian.bau" <(E-Mail Removed)>
wrote:
> On May 17, 12:11 pm, Richard Heathfield <(E-Mail Removed)> wrote:
>
> > fclose can fail. Check the return result. (Here, I'm speaking as much to
> > myself as to you!)

>
> The question is: If it fails, what are you supposed to do? fclose can
> obviously fail if I make some programming error, like fclose (fp);
> fclose (fp); where the second call is quite likely to fail. But apart
> from that, what can you do?


Error propagation and catching is a problem in C and in general
structured programming languages.
Coming back to the topic. You can check the errno. Check this:
http://man.he.net/?topic=fclose&section=all

Check this to know how to use errno: http://www.mkssoftware.com/docs/man5/errno.5.asp
http://www.unix.geek.org.uk/~arny/info/library_2.html


 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      05-17-2007
In article <(E-Mail Removed). com>,
christian.bau <(E-Mail Removed)> wrote:

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


>The question is: If it fails, what are you supposed to do? fclose can
>obviously fail if I make some programming error, like fclose (fp);
>fclose (fp); where the second call is quite likely to fail. But apart
>from that, what can you do?


You can warn the user, so he doesn't delete the old version of his
data.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
 
Reply With Quote
 
William Ahern
Guest
Posts: n/a
 
      05-17-2007
Tor Rustad <torust_at_online.no> wrote:
> empriser wrote:
> > FILE *fp;
> >
> > fp = fopen( ... )
> > fclose( fp ).
> >
> >
> > How do I know if fp has been fclose.

>
> fclose() returns 0 on success.


Even on failure fp is still effectively closed for all intents and purposes.
fp becomes (or remains) invalid either way.
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      05-17-2007
christian.bau said:

> On May 17, 12:11 pm, Richard Heathfield <(E-Mail Removed)> wrote:
>
>> fclose can fail. Check the return result. (Here, I'm speaking as much
>> to myself as to you!)

>
> The question is: If it fails, what are you supposed to do? fclose can
> obviously fail if I make some programming error, like fclose (fp);
> fclose (fp); where the second call is quite likely to fail. But apart
> from that, what can you do?


As Richard T points out, you can warn the user. Also, you may still have
the data in memory, in which case you can prompt for a different
filename, suggesting to the user that he might like to specify a
different device.

"Warn the user" is the very least you can do for /any/ failure (except
one). Often you can do more, but even if you can't, at least you can
clue the user in to what is going wrong.

The one exception, of course, is where the failure is that your program
can't write to the output device that your user is monitoring (be it
stderr or some customised dialog routine or whatever). Some days, it
just isn't worth getting out of bed.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      05-17-2007
In article <(E-Mail Removed)>,
Richard Heathfield <(E-Mail Removed)> wrote:

>"Warn the user" is the very least you can do for /any/ failure (except
>one). Often you can do more, but even if you can't, at least you can
>clue the user in to what is going wrong.
>
>The one exception, of course, is where the failure is that your program
>can't write to the output device that your user is monitoring (be it
>stderr or some customised dialog routine or whatever). Some days, it
>just isn't worth getting out of bed.


You could sit in a loop, so that the user has to kill the program.
That should make them think twice about checking the data is intact.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Some people tell that at present, most web hosting servers supportall kinds of programming language, some people tell me that many web hostingserver don't support Java, What is the truth? Erwin Moller Java 3 05-07-2008 05:09 PM
CAN any one tell it whats the code tell it yogesh C++ 1 03-14-2007 01:12 PM
How to tell which row in datagrid has been clicked. Terry Olsen ASP .Net 3 07-12-2005 09:51 AM
Script to Tell Tell Time then Limit by it? Mike A Javascript 17 11-19-2003 02:16 PM



Advertisments