The function rename (), as the name suggests, is used to rename a file. The prototype of the function is : int rename (const char *oldname, const char *newname).Here, oldname specifies the pathname of the file to be renamed and newname gives the new pathname of the file.
RENAMING
THE FILE
The
function rename (), as the name suggests, is used to rename a file. The
prototype of the function is
int rename (const char *oldname,
const char *newname)
Here,
oldname specifies the pathname of the file to be renamed and newname gives the
new pathname of the file. On success, rename () returns zero. In case of error,
it will return a non-zero value and will set the errno to indicate the error.
When an error occurs neither the file named by oldname nor the file named by
newname shall be changed or created.
Points to remember about rename ()
•
If the oldname specifies pathname of a file that is not a directory, the
newname shall also not point to the pathname of a directory.
•
If the oldname specifies the pathname of a directory then the newname shall not
point to the pathname of a file that is not a directory. In this case, if the
directory named by the newname already exists then it shall be removed and
oldname will be renamed to newname.
Look
at the program code given below which illustrates the use of rename ().
#include <stdio.h>
main()
{
int success=0;
success = rename
("comments.txt","feedback.txt");
if (success != 0)
printf("\n The file could not
be renamed");
return 0;
}
Programming in C: Unit V: File processing : Tag: : Syntax with Example C Program - Renaming the File
Programming in C
CS3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation