Using rename() to change a filename. - C File

C examples for File:File Operation

Description

Using rename() to change a filename.

Demo Code

#include <stdio.h>

int main( void )
{
    char oldname[80], newname[80];

    printf("Enter current filename: ");
    gets_s(oldname);//from   w w w.  j av a2s .  co m
    printf("Enter new name for file: ");
    gets_s(newname);

    if ( rename( oldname, newname ) == 0 )
        printf("%s has been renamed %s.\n", oldname, newname);
    else
        fprintf(stderr, "An error has occurred renaming %s.\n", oldname);
    return 0;
}

Result


Related Tutorials