rename() in C | Detail

From the name of the function we can easily understand,  a rename( ) function is basically used to rename the stream(file) in C.

rename functiion in c
rename( ) in C

Through this article we will be going to discuss rename( ) function of C in detail in which following topics are included such as..

  1. rename function in c
  2. return value of rename function in c
  3. header file for rename function in c
  4. rename function in c syntax
  5. rename function in c example

rename function in c

rename( ) funtion is nothing but, it is a standard library function of C and it also called file handling function which is basically used to rename a file. we can give full path also in argument, but the drives should be same although directories may differ.

return value of rename function in C

rename function of C return 0( zero ) on success and return -1( minus one ) and errno is set one of these values.

ENOENT  :   No such file or directory
EACCES   :   Permission denied
ENOTSAM:   No same device 

Header file of rename function in C

This file function is define in <stdio.h> which is stand for standard input-output header file.

Syntax of rename function in C

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

Example of rename function in C

#include<stdio.h>
#include<stdlib.h>
int main()
{
char old_name[80],new_name[80];
printf("Enter the name of file");
gets(old_name);
printf("Enter a new name for the file");
gets(new_name);
if(rename(old_name,new_name)==0)
printf("File %s renamed to %$\n",old_name,new_name);
else
perror("File not renamed");
return 0;
}

Conclusion

rename( ) function of C in file handling is basically used to rename the stream(file) name.

Post a Comment (0)
Previous Post Next Post