C Reference ยท stdio.h
C remove() and rename() functions
Reference for C remove() and rename(): delete or rename a file by name, check the return value and errno, replace existing files safely and avoid common mistakes.
remove() deletes a file by name. rename() gives a file a new name, and moves it if the new name is in another directory of the same file system. Both work on names, not on open streams, and both report failure through their return value and errno.
Together they support a safe way to update a file: write the new content to a temporary file, close it, and then rename() it over the old one. If the program crashes in the middle, the original is still complete.
#include <stdio.h> int remove(const char *path); int rename(const char *old_path, const char *new_path);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
int remove(const char *path); int rename(const char *old_path, const char *new_path);
| Name | Description |
|---|---|
path |
The file to delete. |
old_path |
The existing name of the file. |
new_path |
The new name or location. |
Returns: 0 on success, non-zero on failure with errno set (commonly ENOENT for a missing file, EACCES or EPERM for permissions, EBUSY when the file is in use, EXDEV when rename() would cross file systems).
Errors / undefined behavior: NULL arguments are undefined behavior. Removing or renaming a file that is currently open has system-specific results.
Usage notes
- Check the result. A failed
remove()on a temporary file is not fatal, but a failedrename()during a save means the save did not happen. - If
new_pathalready exists, the standard says the outcome is implementation-defined. On Linux and macOS (POSIX) the old target is replaced atomically. On Windows, the C library'srename()fails if the target exists, so portable code removes the target first or uses a platform call. - Close first. Windows refuses to delete or rename an open file. On POSIX the name disappears at once, but the data stays until the last stream is closed.
- Directories. On POSIX,
remove()deletes an empty directory as well as a file; the standard only promises it for files. For directories preferrmdir()andmkdir()from<unistd.h>/<sys/stat.h>. - Not secure erasing.
remove()removes the name; the bytes can often be recovered. - Race conditions. Testing with
fopen()and then acting on the answer leaves a gap in which another process can change things. For reliable "create only if missing" usefopen(path, "wx"). - Temporary names.
tmpnam()is unsafe for the same reason; usetmpfile()or POSIXmkstemp(). - Relative paths depend on the working directory. Build the temporary file next to the real one (same directory) so
rename()stays on one file system.
Related
fopen,fclose,tmpfile,perror- Lessons: File handling basics, Error handling