...
The following noncompliant code example shows a case where a file is removed while it is still open.
| Code Block |
|---|
|
char *file_name;
FILE *file;
/* initialize file_name */
file = fopen(file_name, "w+");
if (file == NULL) {
/* Handle error condition */
}
/* ... */
if (remove(file_name) != 0) {
/* Handle error condition */
}
/* continue performing I/O operations on file */
fclose(file);
|
...
| 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 file are closed \[[Open Group 2004|AA. Bibliography#Open Group 04]\]. |
| Code Block |
|---|
|
FILE *file;
char *file_name;
/* initialize file_name */
file = fopen(file_name, "w+");
if (file == NULL) {
/* Handle error condition */
}
if (unlink(file_name) != 0) {
/* Handle error condition */
}
/*... continue performing I/O operations on file ...*/
fclose(file);
|
...