std:: rename
From cppreference.net
C++
Input/output library
| 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)
|
C-style I/O
| Types and objects | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Functions | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
定义于头文件
<cstdio>
|
||
|
int
rename
(
const
char
*
old_filename,
const
char
*
new_filename
)
;
|
||
更改文件的文件名。该文件由指向 old_filename 的字符字符串标识。新文件名由指向 new_filename 的字符字符串标识。
如果 new_filename 已存在,其行为由实现定义。
目录 |
参数
| old_filename | - | 指向以空字符结尾的字符串的指针,该字符串包含待重命名文件的路径标识 |
| new_filename | - | 指向以空字符结尾的字符串的指针,该字符串包含文件的新路径 |
返回值
0 表示成功,非零值表示错误。
注释
POSIX 标准详细规定了此函数的诸多语义细节,这些细节在 C++ 中通过 std::filesystem::rename 得以实现。
示例
运行此代码
#include <cstdio> #include <cstdlib> #include <fstream> #include <iostream> int main() { if (!std::ofstream("from.txt").put('a')) // 创建文件并写入内容 { std::perror("Error creating from.txt"); return EXIT_FAILURE; } if (std::rename("from.txt", "to.txt")) { std::perror("Error renaming"); return EXIT_FAILURE; } std::cout << std::ifstream("to.txt").rdbuf() << '\n'; // 打印文件内容 return EXIT_SUCCESS; }
输出:
a
参见
|
(C++17)
|
移动或重命名文件/目录
(函数) |
|
删除文件
(函数) |
|
|
C 文档
for
rename
|
|