RAD Studio
ContentsIndex
PreviousUpNext
rename, _wrename

Header File 

stdio.h  

Category 

Input/output Routines 

Prototype 

int rename(const char *oldname, const char *newname); 

int _wrename(const wchar_t *oldname, const wchar_t *newname); 

Description 

Renames a file. 

rename changes the name of a file from oldname to newname. If a drive specifier is given in newname, the specifier must be the same as that given in oldname. 

Directories in oldname and newname need not be the same, so rename can be used to move a file from one directory to another. Wildcards are not allowed. 

This function will fail (EEXIST) if either file is currently open in any process. 

Return Value 

On success, rename returns 0.  

On error (if the file cannot be renamed), it returns -1 and the global variable errno is set to one of the following values:

EEXIST 
Permission denied: file already exists. 
ENOENT 
No such file or directory 
ENOTSAM 
Not same device 

Example

#include <stdio.h>
int main(void)
{
    char oldname[80], newname[80];
    /* prompt for file to rename and new name */
    printf("File to rename: ");
    gets(oldname);
    printf("New name: ");
    gets(newname);
    /* Rename the file */
    if (rename(oldname, newname) == 0)
       printf("Renamed %s to %s.\n", oldname, newname);
    else
       perror("rename");
    return 0;
}

Portability

 
POSIX 
Win32 
ANSI C 
ANSI C++ 
rename 
_wrename 
 
NT only 
 
 
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!