C66. C Miscellaneous File Operations


Miscellaneous File Operations
int remove(const char *filename);
int rename(const char *old, const char *new);
The functions remove and rename allow a program to perform basic tile man­agement operations. Unlike most other functions in this section, remove and rename work with file names instead of file pointers. Both functions return zero if they succeed and a nonzero value if they fail, remove deletes a file:
remove("foo"); /* deletes the file named "foo" */
If a program uses fopen (instead of tmpfile) to create a temporary file, it can use remove to delete the file before the program terminates. Be sure that the file to be removed has been closed; the effect of removing a file that's currently open is implementation-defined.
rename changes the name of a file:
rename("foo", "bar");  /* renames "foo" to "bar" */
rename is handy for renaming a temporary file created using fopen if a program should decide to make it permanent. If a file with the new name already exists, the effect is implementation-defined.
If the tile to be renamed is open, be sure to close it before calling rename; the function may fail if asked to rename an open file.

No comments: