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




// C program to demonstrate use of rename()
#include <stdio.h>
 
int main()
{
    // Old file name
    char old_name[] = "geeks.txt";
 
    // Any string
    char new_name[] = "w3wiki.txt";
    int value;
 
    // File name is changed here
    value = rename(old_name, new_name);
 
    // Print the result
    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


rename function in C

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.

Similar Reads

Syntax of rename()

int rename (const char *old_name, const char *new_name);...

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....