Home »
C programs »
C stdio.h library functions programs
rename() function in C language with Example
Here, we are going to learn about the rename() function of library function stdio.h in C language with its syntax, example.
Submitted by Souvik Saha, on February 28, 2019
rename() function in C
The rename() function is defined in the <stdio.h> header file.
Prototype:
int rename(const char* old-filename, const char* new-filename);
Parameters: const char* old-filename, const char* new-filename
Return type: int
Use of function:
When we are dealing with files then sometimes we need to rename some files. In file handling, we use rename() function to rename the files. The prototype of the function rename() is int rename(const char* old-filename, const char* new-filename);
Here, old-filename is the previous name of the file which has to renamed with the new name as new-filename. It returns zero if the file is successfully renamed and non-zero if an error has occurred.
rename() example in C
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE* f;
//Check the existence of that file
if ((f = fopen("includehelp.txt", "r")) == NULL) {
printf("File does not exist...\n");
}
else {
printf("File is exist.\n");
}
fclose(f);
//Rename the file
if (rename("includehelp.txt", "new_includehelp.txt"))
printf("Rename error.....\n");
else
printf("File is renamed\n");
if ((f = fopen("includehelp.txt", "r")) == NULL) {
printf("File does not exist...\n");
}
else {
printf("File is exist.\n");
}
fclose(f);
if ((f = fopen("new_includehelp.txt", "r")) == NULL) {
printf("File does not exist...\n");
}
else {
printf("File is exist.\n");
}
fclose(f);
return 0;
}
Output
C stdio.h Library Functions Programs »