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

S

shyam

Hi All

I had raised a simillar query in an earlier post

http://groups.google.com/group/comp...fstream+write+failure&rnum=1#1d054627402539e1

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
 
R

Roland Pibinger

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
 
B

blytkerchan

shyam said:
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
 
T

tolgaceylanus

blytkerchan said:
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
 
T

tolgaceylanus

blytkerchan said:
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
 
S

shyam

Roland said:
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.
 
T

tolgaceylanus

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top