| Types and objects | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Defined in header <stdio.h> | ||
int rename(constchar* old_filename,constchar* new_filename); | ||
Changes the filename of a file. The file is identified by character string pointed to byold_filename. The new filename is identified by character string pointed to bynew_filename.
Ifnew_filename exists, the behavior is implementation-defined.
Contents |
| old_filename | - | pointer to a null-terminated string containing the path identifying the file to rename |
| new_filename | - | pointer to a null-terminated string containing the new path of the file |
0 upon success or non-zero value on error.
POSIX specifies many additional details on the semantics of this function.
#include <stdio.h>#include <stdlib.h> int main(void){FILE* fp=fopen("from.txt","w");// create file "from.txt"if(!fp){perror("from.txt");returnEXIT_FAILURE;}fputc('a', fp);// write to "from.txt"fclose(fp); int rc= rename("from.txt","to.txt");if(rc){perror("rename");returnEXIT_FAILURE;} fp=fopen("to.txt","r");if(!fp){perror("to.txt");returnEXIT_FAILURE;}printf("%c\n",fgetc(fp));// read from "to.txt"fclose(fp); returnEXIT_SUCCESS;}
Possible output:
a
| erases a file (function)[edit] | |
C++ documentation forrename | |