Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Wiki Markup
Invoking {{remove()}} on an open file is [implementation-defined|BB. Definitions#implementation-defined behavior]. Consequently, care must be taken when {{remove()}} is called on an open file. Removing an open file is sometimes recommended to hide the names of temporary files that may prone to attack (see \[[TMP30-C. Temporary files must created with unique and unpredictable file names]]). 

In these cases, the intended implementations need to be considered and an alternate, more strongly defined function, such as The Open Group's {{unlink()}} should be used. To be strictly conforming and portable, {{remove()}} should _not_ be called on an open cases when an open file needs to be removed, a more strongly defined function, such as the POSIX unlink() function, should be considered. To be strictly conforming and portable, remove() should not be called on an open file.

Non-Compliant Code Example

...

Code compiled using Microsoft Visual Studio C++ 2005 and run on Microsoft Windows XP, prevents the remove() call from succeeding when the file is open, meaning that the file link will remain after execution completes.

Compliant Solution

...

(POSIX)

Wiki Markup
This compliant solution uses the POSIX {{unlink()}} function to remove the file.  The {{unlink()}} function  is guaranteed  to unlink the file from the file system hierarchy but keep the file on disk until all open instances of the fie are closed) is used \[[Open Group 04|AA. C References#Open Group 04]\]

The compliant solution depends on programmer intent. If the programmer simply neglected to close the file before attempting to remove it, the file can simply be closed before the remove is attempted.

Code Block
bgColor#ccccff

FILE *file;

/* ... */

file = fopen("myfile", "w+");
if (fopen == NULL) {
  /* Handle error condition */
}

/* ... */

fclose("myfile");
remove("myfile");

It is not possible to (portably) remove the link from the file system while the file is open using remove(). While this may be possible on some implementations, it is important to document the dependency on implementation-defined behavior.

Compliant Solution (POSIX)

In this compliant solution intended for POSIX environments, The Open Group's unlink() function (which is guaranteed by The Open Group Base Specifications Issue 6 to unlink the file from the file system hierarchy but keep the file on disk until all open instances of it are closed) is used

.

Code Block
bgColor#ccccff
#include <unistd.h>

FILE *file;

/* ... */

file = fopen("myfile", "w+");
if (fopen == NULL) {
  /* Handle error condition */
}
unlink("myfile");
fclose("myfile");

...

Wiki Markup
\[[ISO/IEC 9899-1999|AA. C References#ISO/IEC 9899-1999]\] Section 7.19.4.1, "The remove function"
\[[Open Group 04|AA. C References#Open Group 04]\] [{{unlink()}}|http://www.opengroup.org/onlinepubs/000095399/functions/unlink.html]

...

FIO07-A. Prefer fseek() to rewind()      09. Input Output (FIO)       FIO09-A. fflush() should be called after writing to an output stream if data integrity is important