944,150 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 7483
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
May 8th, 2005
0

ofstream - detect if file has been deleted between open and close

Expand Post »
As seen in another thread, I'm working on a Log class. I'm running into an issue that if I delete the file that's being written to, the Log class doesn't detect it and continues to 'think' it's writing even though the file is gone... (Note that this is on sun solaris in case it matters)

I've tried the following to try and detect this
(assuming my ofstream is named out)
!out.is_open()
!out.good()
out.fail()

None of them seem to detect this issue. (Nor changing the permissions to non-write while the file has already been opened by the app). Note that these functions DO catch a problem with opening the file, but not when it's already opened. Any advice?
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
May 8th, 2005
0

Re: ofstream - detect if file has been deleted between open and close

Quote originally posted by winbatch ...
As seen in another thread, I'm working on a Log class. I'm running into an issue that if I delete the file that's being written to, the Log class doesn't detect it and continues to 'think' it's writing even though the file is gone... (Note that this is on sun solaris in case it matters)

I've tried the following to try and detect this
(assuming my ofstream is named out)
!out.is_open()
!out.good()
out.fail()

None of them seem to detect this issue. (Nor changing the permissions to non-write while the file has already been opened by the app). Note that these functions DO catch a problem with opening the file, but not when it's already opened. Any advice?
Why not close the file descriptor?
Reputation Points: 44
Solved Threads: 1
Junior Poster
subtronic is offline Offline
117 posts
since Aug 2003
May 8th, 2005
0

Re: ofstream - detect if file has been deleted between open and close

Not sure I follow. I have a program that is writing/appending to a file every second (as a test). In another window/session, I'm deleting that file via the rm command in unix. I want my program to detect that it can no longer write to the file. instead, it just keeps on writing. (Though nothing is actually written as the file is gone)
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
May 8th, 2005
0

Re: ofstream - detect if file has been deleted between open and close

Quote originally posted by winbatch ...
Not sure I follow. I have a program that is writing/appending to a file every second (as a test). In another window/session, I'm deleting that file via the rm command in unix. I want my program to detect that it can no longer write to the file. instead, it just keeps on writing. (Though nothing is actually written as the file is gone)
So, in *nix, when you open a file descriptor, you grab a reference to that file. Thus, when you unlink(2) that file using the "rm" command, you're only dropping that reference. Because you have a stream open (and another reference), the file will not actually be removed [yet]. Once you close the stream, you will release that reference, and presumably at that point the file's reference count will then drop to 0 and be "removed".

Ah reference counting, fun for the entire family.
Reputation Points: 44
Solved Threads: 1
Junior Poster
subtronic is offline Offline
117 posts
since Aug 2003
May 8th, 2005
0

Re: ofstream - detect if file has been deleted between open and close

What you want to do is not possible in standard C++. You seem to think that a file stream knows if the external device is available if you remove it between construction and destruction of the stream. In reality, all you're doing is invoking undefined behavior by writing to a device that you've told the stream exists and will continue to exist until destruction. That's an implied contract when you open a file stream and you're not adhering to it.

My kneejerk reaction to this problem is to tell you not to delete the file until you're done with it. However, you can do a platform dependent test to see if the file exists before every operation on it. However, that's inefficient and just sweeps the dirt under the rug.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
May 8th, 2005
0

Re: ofstream - detect if file has been deleted between open and close

I was only deleting it to see if my class would detect it. So what you're saying is that it can't detect the deletion, or a change in permissions, etc. What about running out of disk space?
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
May 8th, 2005
0

Re: ofstream - detect if file has been deleted between open and close

Quote originally posted by winbatch ...
I was only deleting it to see if my class would detect it. So what you're saying is that it can't detect the deletion, or a change in permissions, etc. What about running out of disk space?
So, if you're writing a program that's specific to a "unix" system why not use C and OS libraries?? And, like I told you, your concept and understanding of "deletion" is wrong. You can't detect deletion because nothing is BEING deleted.
Reputation Points: 44
Solved Threads: 1
Junior Poster
subtronic is offline Offline
117 posts
since Aug 2003
May 8th, 2005
0

Re: ofstream - detect if file has been deleted between open and close

I'm not using C because my Log class uses inheritance to either be a screen, or a file (and eventually a socket, or a database, etc..)

By the way, if not for detecting problems writing, what's the point of the .bad() and .fail() functions? When would they be used?
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
May 9th, 2005
0

Re: ofstream - detect if file has been deleted between open and close

>So what you're saying is that it can't detect the deletion, or a change in permissions, etc
Did I studder?
Quote originally posted by Narue ...
What you want to do is not possible in standard C++.
>What about running out of disk space
That will cause failbit (and possibly badbit) to be set.

>By the way, if not for detecting problems writing, what's the point of the .bad() and .fail() functions?
You're confusing errors during legitimate use with blatant and intentional breaking of the rules.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
May 9th, 2005
0

Re: ofstream - detect if file has been deleted between open and close

You certainly didn't 'studder', you might have 'stuttered'

In any case, I'm not expecting blatant breaking of the rules, I'm only trying to emulate failure conditions to test the code.
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: help me to correct this part of my program
Next Thread in C++ Forum Timeline: Cubed root function?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC