Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c

How to use rename function in C

Behzad Ahmad

The rename() function in C++ renames a specified file. It takes two arguments:

  1. oldname
  2. newname and returns an integer value.

It renames a file in which a string points to an old name to a string pointed by a new name.

The syntax is as follows:

int rename(const char *oldname , const char *newname);

Standard library

include<cstdio>

Parameters

  1. oldname: Points to the string containing the old name.

  2. newname: Points to the string containing the new name.

Code

main.c
old.txt
#include <stdio.h>

int main() 
{
  char oldname[] = "old.txt";
  char newname[] = "new.txt";

  if ( rename ( oldname , newname) != 0 )
  {
    printf ("Error Renaming File!!!");
  }    
  else
  {
    printf("File Renamed Successfully");
  }   
  return 0;
}

RELATED TAGS

c

CONTRIBUTOR

Behzad Ahmad
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring