Name
rename - Renames a file or directory.
Syntax
#include <stdio.h>
#include <io.h>
int rename(oldname, newname)
const char *oldname;
const char *newname;
Description
The rename function renames the file or directory specified
by oldname to the name given by newname. The old name must
be the path name of an existing file or directory. The new
name must not be the name of an existing file or directory.
The rename function can be used to move a file from one
directory to another by giving a different path name in the
newname argument. However, files cannot be moved from one
device to another (for example, from drive A to drive B).
Directories can only be renamed, not moved.
Notes
Note that the order of arguments in rename is the reverse of
their order in versions of the Microsoft C Compiler prior to
4.0. This change was made to conform to the proposed ANSI C
standard.
Return Value
The rename function returns 0 if it is successful. On an
error, it returns a nonzero value and sets errno to one of
the following values:
Value Meaning
EACCES File or directory specified by newname already
exists or could not be created (invalid path); or
oldname is a directory and newname specifies a
different path.
ENOENT File or path name specified by oldname not found.
EXDEV Attempt to move a file to a different device.
See Also
creat(DOS), fopen(DOS), open(DOS)
Example
#include <stdio.h>
main()
{
int result;
/* Attempt to rename file: */
result = rename("input","data");
/* Check for errors: */
if (result != 0)
perror("Was not able to rename file");
else
printf("File successfully renamed\n");
}
This program uses rename to rename a file named input to
data. For this operation to succeed, a file named input must
exist and a file named data must not exist.
(printed 6/18/89)