C – Rename and Remove File

Renaming the File:

  • rename() function is used to rename the specified file.
  • On success, it returns 0
  • On failure, it returns -1

int rename(char* old, char* new);

#include<stdio.h>
int main()
{
            char old[20] = “data.txt”;
            char new[20] = “modified_data.txt”;
           
            if(rename(old, new)==0)
                        printf(“successfully renamed…\n”);
            else
                        printf(“Failed in renaming the file…\n”);
            return 0;
}

Remove the file from specified location:

  • remove() is used to rename the specified file.
  • On success, it returns 0
  • On failure, it returns -1

int remove(char* target);

#include<stdio.h>
int main()
{
            char target[20] = “data.txt”;
           
            if(remove(target)==0)
                        printf(“successfully removed…\n”);
            else
                        printf(“Failed in removing the file…\n”);
            return 0;
}
Scroll to Top