rename function in C
Last Updated :
21 Sep, 2023
The rename() function is used to rename a file in C. It changes the name of the file from old_name to new_name without modifying the content present in the file. It is defined inside <stdio.h> header file.
In this article, we will learn how to rename a file using the rename() function in C programming language.
Syntax of rename()
int rename (const char *old_name, const char *new_name);
If new_name is the name of an existing file in the same folder then the function may either fail or override the existing file, depending on the specific system and library implementation.
Parameters
- old_name: String that represents the name of an existing file to be renamed.
- new_name: String containing the new name of the file.
Return Value
The return type of the function is an integer.
- If the file is renamed successfully, zero is returned and it prints a success message.
- On failure, a nonzero value is returned and it prints an error message using perror().
Example of rename()
Assume that we have a text file having the name geeks.txt, having some content. So, we are going to rename this file, using the below C program present in the same folder where this file is present.
C Program to Demonstrate the use of rename() Function
C
#include <stdio.h>
int main()
{
char old_name[] = "geeks.txt" ;
char new_name[] = "geeksforgeeks.txt" ;
int value;
value = rename (old_name, new_name);
if (!value) {
printf ( "%s" , "File name changed successfully" );
}
else {
perror ( "Error" );
}
return 0;
}
|
Output
If file name changed
File name changed successfully
OR
If file is not present
Error: No such file or directory
![filename after running code](https://media.geeksforgeeks.org/wp-content/uploads/After_running_code-1.png)
Share your thoughts in the comments
Please Login to comment...