Defined in header <stdio.h> | ||
---|---|---|
int rename( const char *old_filename, const char *new_filename ); |
Changes the filename of a file. The file is identified by character string pointed to by old_filename
. The new filename is identified by character string pointed to by new_filename
.
If new_filename
exists, the behavior is implementation-defined.
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> int main(void) { FILE* fp = fopen("from.txt", "w"); // create file "from.txt" if(!fp) { perror("from.txt"); return 1; } fputc('a', fp); // write to "from.txt" fclose(fp); int rc = rename("from.txt", "to.txt"); if(rc) { perror("rename"); return 1; } fp = fopen("to.txt", "r"); if(!fp) { perror("to.txt"); return 1; } printf("%c\n", fgetc(fp)); // read from "to.txt" fclose(fp); }
Output:
a
erases a file (function) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/c/io/rename