By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,441 Members | 1,446 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,441 IT Pros & Developers. It's quick & easy.

How to detect the deletion of file which is opened through ofstream??

P: n/a
Hi All

I had raised a simillar query in an earlier post

http://groups.google.com/group/comp....054627402539e1

Basically
I have a file which is opened through ofstream for writing.

Now I want to check if the file has been deleted or not, while being in
the opened state.

I had the notion that if the file is deleted then any subsequent write
on it wil set the fail or bad bit
or raise a failure exception. But this is not the case.

Also I tried all the options suggested in the earlier post but nothing
works.

After getting frustrated I decided to do it the C way using fopen &
fputs etc..

but surprisingly even then I cant detect the file deletion. fputs does
not return any error is I write to a file which is deleted

I would appreciate if anyone can guide me on this.
Can it be done the ofstream way ? and if not then how can we acheive
this?

thanks in advance

Dec 27 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On 27 Dec 2006 06:40:57 -0800, "shyam" wrote:
>Basically
I have a file which is opened through ofstream for writing.
Now I want to check if the file has been deleted or not, while being in
the opened state.
Seems to be a design problem ...
>I had the notion that if the file is deleted then any subsequent write
on it wil set the fail or bad bit
or raise a failure exception. But this is not the case.
ofstream and stdio functions are buffered. You most probably get an
error when the buffer is flushed (written to the file).

Best wishes,
Roland Pibinger
Dec 27 '06 #2

P: n/a
shyam wrote:
Basically
I have a file which is opened through ofstream for writing.
Now I want to check if the file has been deleted or not, while being in
the opened state.
Have a look at Boost.Filesystem and wrap your ofstream into a class
that, whenever you try to write to the file, checks its existance
first.

Still looks like a design problem, though..

rlc

Dec 27 '06 #3

P: n/a

blytkerchan wrote:
shyam wrote:
Basically
I have a file which is opened through ofstream for writing.
Now I want to check if the file has been deleted or not, while being in
the opened state.
Have a look at Boost.Filesystem and wrap your ofstream into a class
that, whenever you try to write to the file, checks its existance
first.

Still looks like a design problem, though..

rlc
The problem is OS/filesystem specific AFAIK.

One way to tackle this is to check for filename if it exists before
each write and reopen (also you might want to check if inode numbers
match to see if another file with the same name was created). Note that
this is not an optimal solution due to these expensive system calls.

Some filesystems such as ext3, nfs will preserve the open but deleted
files until the process keeping the filehandles terminates, so the
application can write to that file even if it's deleted. (This also
explains those mysterious mismatches between 'du' and 'df' commands if
an application is keeping a 2GB deleted log file open. :-)))

Hope this helps,

Tolga Ceylan

Dec 27 '06 #4

P: n/a

blytkerchan wrote:
shyam wrote:
Basically
I have a file which is opened through ofstream for writing.
Now I want to check if the file has been deleted or not, while being in
the opened state.
Have a look at Boost.Filesystem and wrap your ofstream into a class
that, whenever you try to write to the file, checks its existance
first.

Still looks like a design problem, though..

rlc
The problem is OS/filesystem specific AFAIK.

One way to tackle this is to check for filename if it exists before
each write and reopen (also you might want to check if inode numbers
match to see if another file with the same name was created). Note that
this is not an optimal solution due to these expensive system calls.

Some filesystems such as ext3, nfs will preserve the open but deleted
files until the process keeping the filehandles terminates, so the
application can write to that file even if it's deleted. (This also
explains those mysterious mismatches between 'du' and 'df' commands if
an application is keeping a 2GB deleted log file open. :-)))

Hope this helps,

Tolga Ceylan

Dec 27 '06 #5

P: n/a

Roland Pibinger wrote:
On 27 Dec 2006 06:40:57 -0800, "shyam" wrote:
Basically
I have a file which is opened through ofstream for writing.
Now I want to check if the file has been deleted or not, while being in
the opened state.

Seems to be a design problem ...
You mean .. it is a design problem with ofstream??

My requirement is not to check for file deletion every time,,. I just
want that if at all an opened file is deleted then during the next
write operation I get notified so that I can create the file again and
continue the write operations.

>
I had the notion that if the file is deleted then any subsequent write
on it wil set the fail or bad bit
or raise a failure exception. But this is not the case.

ofstream and stdio functions are buffered. You most probably get an
error when the buffer is flushed (written to the file).

Best wishes,
Roland Pibinger
Dec 28 '06 #6

P: n/a

>
You mean .. it is a design problem with ofstream??

My requirement is not to check for file deletion every time,,. I just
want that if at all an opened file is deleted then during the next
write operation I get notified so that I can create the file again and
continue the write operations.

The design problem is the description above. Some flaw in the design
forces you to check for the file everytime. Why does the open file get
deleted? This is the real problem.

Tolga Ceylan

Dec 28 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.