| I/O manipulators | ||||
| Print functions(C++23) | ||||
| C-style I/O | ||||
| Buffers | ||||
(C++23) | ||||
(C++98/26*) | ||||
(C++20) | ||||
| Streams | ||||
| Abstractions | ||||
| File I/O | ||||
| String I/O | ||||
| Array I/O | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
(C++98/26*) | ||||
(C++98/26*) | ||||
(C++98/26*) | ||||
| Synchronized Output | ||||
(C++20) | ||||
| Types | ||||
| Error category interface | ||||
(C++11) | ||||
(C++11) |
| Types and objects | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Functions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Defined in header <cstdio> | ||
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, which are reproduced in C++ bystd::filesystem::rename.
#include <cstdio>#include <cstdlib>#include <fstream>#include <iostream> int main(){if(!std::ofstream("from.txt").put('a'))// create and write to file{std::perror("Error creating from.txt");returnEXIT_FAILURE;} if(std::rename("from.txt","to.txt")){std::perror("Error renaming");returnEXIT_FAILURE;} std::cout<<std::ifstream("to.txt").rdbuf()<<'\n';// print filereturnEXIT_SUCCESS;}
Output:
a
(C++17) | moves or renames a file or directory (function)[edit] |
| erases a file (function)[edit] | |
C documentation forrename | |