How to use rename function in C

The rename() function in C++ renames a specified file. It takes two arguments:

  1. oldname
  2. newname and returns an integer value.

It renames a file in which a string points to an old name to a string pointed by a new name.

The syntax is as follows:

int rename(const char *oldname , const char *newname);

Standard library

include<cstdio>

Parameters

  1. oldname: Points to the string containing the old name.

  2. newname: Points to the string containing the new name.

Code

main.c
old.txt
#include <stdio.h>
int main()
{
char oldname[] = "old.txt";
char newname[] = "new.txt";
if ( rename ( oldname , newname) != 0 )
{
printf ("Error Renaming File!!!");
}
else
{
printf("File Renamed Successfully");
}
return 0;
}

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved