Re: Behavior of fwrite(), fseek(), fflush() if the file gets deleted after being opened.



Aditya <adityagupta.18@xxxxxxxxx> writes:
I am using Red Hat linux 9 and gcc 3.4.
I am writing an application in C with file handling. The application
opens a file(creates it at run time) using fopen() in binary write
mode. The file is open until the application exits.

The application starts and opens(creates) a file, and waits for an
input to be written to the file.
I delete the file using rm -f <filename> as soon as the application
starts before I give it an input to the application(just to clarify, I
delete it from a different terminal, without stopping the application
to wait for an input).
Now I give the input and debug through the application, I find that
the fseek(), fwrite(), fflush() calls in the application for writing
to the file do not throw error even though the file is not existant.
I cannot understand, where is that data written. If it is wriiten to
the buffer, then fflush() should have reported error. But it also
succeeds.
[...]

The C standard doesn't specify what happens when one process is access
a file and another processes deletes that file. In fact, the C
standard doesn't even deal with multiple processes at all.

As it happens, the behavior you're seeing is typical of Unix-like
systems. <OT>You didn't really delete the file; you merely unlinked
it.</OT> For more information, try comp.unix.programmer.

[Reply e-mailed as well as posted.]

--
Keith Thompson (The_Other_Keith) <kst-u@xxxxxxx>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
--
comp.lang.c.moderated - moderation address: clcm@xxxxxxxxxxxx -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
.



Relevant Pages