C programming rename stdio.h

In C programming the <stdio.h> rename function rename the file.Use this function if you want to rename the file.The declaration of the function is given below.

int rename(const char *old, const char *new);

old -The old name of the file to be renamed.

new -The new name of the file.

Return type
int -Return 0 if the operation is successful ,if it fails returns non-zero value.

Some points to note:

i)After calling ‘rename’ we cannot use the ‘old’ name to access the file.

ii) If the ‘new’ file name exist prior to the ‘rename’ the behavior is implementation defined.

ii)Renaming the file does not change the content of the file.

Code example

int result=rename( “text.txt” , “new text.txt” );

if( result==0 )
printf(“File rename successful”);


File rename successful

Here is another program.

Code example

FILe *fp=fopen( “text.txt” , “r” );

int result=rename( “text.txt” , “new text.txt”); //implementation-defined

printf(“File rename successful”);
printf( “File rename not successful” );

fclose( fp );


File rename not successful

Related links

->C fopen stdio.h

->C fclose stdio.h

->C remove stdio.h

->C tmpnam stdio.h