rename() function is used to change the name of the file or directory i.e. from old_name to new_name without changing the content present in the file. This function takes name of the file as its argument.
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.
int rename (const char *old_name, const char *new_name); Parameters: old_name : Name of an existing file to be renamed. new_name : String containing new name of the file.
Return type of function is an integer. If the file is renamed successfully, zero is returned. On failure, a nonzero value is returned.
Assume that we have a text file having 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.
If file name changed File name changed successfully OR If file name not changed Error: No such file or directory
- C/C++ Tricky Programs
- Basics of File Handling in C
- fsetpos() (Set File Position) in C
- tellp() in file handling with c++ with example
- std::strncmp() in C++
- tmpfile() function in C
- fgetc() and fputc() in C
- Chrono in C++
- Database Connectivity using C/C++
- Arrays in C/C++
- C program to delete a file
- C Program to merge contents of two files into a third file
- fseek() vs rewind() in C
- Catching base and derived classes as exceptions
- When should we write our own copy constructor?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.