Rename a file
#include <stdio.h>
int rename( const char* old,
const char* new );
libc
The rename() function renames the file indicated old
to the name specified in new.
If a file (or empty directory) named new exists, it's overwritten.
- 0
- Success.
- Nonzero
- An error occurred (errno is set ).
- EACCESS
- The calling program doesn't have permission to search one of the
components of either path prefix, or one of the directories
containing old or new denies write permission.
- EBUSY
- The directory named by old or new cannot be
renamed because it is in use by another process.
- EEXIST
- The file specified by new is directory that contains files.
- EINVAL
- The new directory pathname contains the old
directory.
- EISDIR
- The file specified by new is a directory and old is a file.
- ELOOP
- Too many levels of symbolic links.
- EMLINK
- The file named by old is a directory, and the link count
of the parent directory of new would exceed
LINK_MAX.
- ENAMETOOLONG
- The length of old or new exceeds
PATH_MAX.
- ENOENT
- The old file doesn't exist, or old or new
is an empty string.
- ENOSPC
- The directory that would contain new cannot be
extended.
- ENOSYS
- The rename() function isn't implemented for the filesystem specified
in old or new.
- ENOTDIR
- A component of either path prefix isn't a directory, or
old is a directory and new isn't.
- ENOTEMPTY
- The file specified by new is a directory that contains files.
- EROFS
- The rename() would affect files on a read-only filesystem.
- EXDEV
- The files or directories named by old and new
are on different filesystems.
#include <stdio.h>
#include <stdlib.h>
int main( void )
{
if( rename( "old.dat", "new.dat" ) ) {
puts( "Error renaming old.dat to new.dat." );
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
ANSI
Safety: | |
Cancellation point |
Yes |
Interrupt handler |
No |
Signal handler |
Yes |
Thread |
Yes |
errno