by Dinesh Thakur Category: File Handling

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*/
   if(fptr ==NULL) /* check if not opened*/
        printf("File could not be opened");
        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);
                  printf("The file with name Myfile is open for appending.\n");
} //close file stream

Function rename () in C

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.