Standard Library Functions in C

adplus-dvertising
rename() Function
Previous Home Next

Description

The C library function, The rename( ) function changes the name of the file specified by old_filename to new_filename. The new_filename must not match any existing directory entry.

Declaration

Following is the declaration for rename() function.

int rename(const char *old_filename, const char *new_filename)

Parameters

old_filename - The old file name that will be changed.

new_filename - The new file name to use.

Return Value

If the file is successfully renamed, a zero value is returned. On failure, a nonzero value is returned.

Example

#include <stdio.h>

int main ()
{
   int ret;
   char oldname[] = "a.txt";
   char newname[] = "b.txt";
   
   ret = rename(oldname, newname);
	
   if(ret == 0) 
   {
      printf("File renamed successfull");
   }
   else 
   {
      printf("unable to rename the file ");
   }
   
   return(0);
}

Output

File renamed successfull
Previous Home Next