Latest web development tutorials

C library functions - rename ()

C standard library - <stdio.h> C standard library - <stdio.h>

description

C library functionsint rename (const char * old_filename, const char * new_filename) the file name old_filenamepointed tonew_filename.

statement

Here is the rename () function's declaration.

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

parameter

  • old_filename - This is a C string containing the to be renamed / moved file name.
  • new_filename - This is a C string that contains the new file name.

return value

If successful, it returns zero. If an error, it returns -1 and set errno.

Examples

The following example demonstrates the rename () function is used.

#include <stdio.h>

int main ()
{
   int ret;
   char oldname[] = "file.txt";
   char newname[] = "newfile.txt";
   
   ret = rename(oldname, newname);

   if(ret == 0) 
   {
      printf("文件重命名成功");
   }
   else 
   {
      printf("错误:不能重命名该文件");
   }
   
   return(0);
}

Suppose we have a text filefile.txt, it reads as follows.We will use the above procedure to rename the file. Let's compile and run the above program, which will generate the following message, and the file is renamednewfile.txt file.

文件重命名成功

C standard library - <stdio.h> C standard library - <stdio.h>