This function is used to change the name of file. Its prototype may be written as given below.
int rename(const char* Old_name, const char * New_name);
After the operation of this function, the file is no longer accessible with the old name. It becomes accessible with the new name only.
Illustrates rename () functions
#include <stdio.h> #include <stdlib.h> void main() { FILE* fptr; // declaration of filepointer fptr = fopen("Student_file","w"); /*opens a filefor writing*/ clrscr(); if(fptr ==NULL) /* check if not opened*/ { printf("File could not be opened"); exit(1); } else printf("File Student file is open for writing.\n"); rename ("Student_file", "Teacher_file"); /* rename the file*/ fptr = fopen("Teacher_file", "a"); /*open with new name*/ if(fptr ==NULL) /* check for failure */ printf("File with name Teacher file not accessible.\n"); printf ("The file is now accessible with 'name Teacher _file.\n") ; rename ("Teacher_file", "Myfile"); /*The above code Changes file name to Myfile.*/ if(fptr== NULL) printf( "The file not accessible."); fptr = fopen ("Myfile", "a"); //open Myfile for appending if(fptr ==NULL) { printf("Myfile is not accessible"); exit (1); } else printf("The file with name Myfile is open for appending.\n"); fclose(fptr); } //close file stream