Home » C programs » stdio.h header file functions

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

rename() example in C language

rename() example in C language






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.