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);

Parameters:
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”);
}

Output,

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

if(result==0)
{
printf(“File rename successful”);
}
else
printf( “File rename not successful” );

fclose( fp );

Output,

File rename not successful


Related links

->C fopen stdio.h

->C fclose stdio.h

->C remove stdio.h

->C tmpnam stdio.h