### rename -- Rename File

Format

>     #include <stdio.h>  /* also in <io.h> */
>     int rename(const char *oldname, const char *newname);

Language Level: ANSI, POSIX, XPG4
rename renames the file specified by oldname to the name given by newname. The oldname pointer must specify the name of an existing file. The newname pointer must not specify the name of an existing file. Both oldname and newname must be on the same drive; you cannot rename files across drives. You cannot rename a file with the name of an existing file. You also cannot rename an open file.

Return Value
rename returns 0 if successful. On an error, it returns a nonzero value.

Example
This example uses rename to rename a file. It issues a message if errors occur.

    #include <stdio.h>

    #if (1 == __TOS_OS2__)
       #define FILENAM1 "oldfile.dat"       /* OS/2 name */
       #define FILENAM2 "newfile.dat"       /* OS/2 name */
    #else
       #define FILENAM1 "oldfile.dat"    /* Windows name */
       #define FILENAM2 "newfile.dat"    /* Windows name */
    #endif

    int main(void)
    {
        char *OldName = FILENAM1;
        char *NewName = FILENAM2;
        FILE *fp;
        fp = fopen(OldName, "w");
        fprintf(fp, "Hello World\n");
        fclose(fp);
        if (rename(OldName, NewName) != 0)
            perror("Could not rename file");
        else
        printf( "File \"%s\" is renamed to  \"%s\".\n",
               OldName, NewName);
        return 0;

        /***************************************************
           The output should be:

           File "oldName" is renamed to "FILENAME"
      *****************************************************/
    }

fopen -- Open Files
remove -- Delete File
<stdio.h>