std::rename
Defined in header <cstdio>  |   |   | 
|---|---|---|
int rename( const char *old_filename, const char *new_filename );  |   |   | 
更改文件的文件名。所指向的字符串标识该文件。old_filename。新文件名由指向的字符串标识。new_filename...
如果new_filename存在时,行为是实现定义的。
参数
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如果成功或错误值为非零。
注记
POSIX指定有关此函数的语义的许多附加细节,这些信息在C++中由std::实验性::文件系统::重命名...
例
二次
#include <iostream>
#include <fstream>
#include <cstdio>
int main()
{
    bool ok = std::ofstream("from.txt").put('a'); // create and write to file
    if(!ok) { std::perror("Error creating from.txt"); return 1; }
 
    int rc = std::rename("from.txt", "to.txt"); 
    if(rc) { std::perror("Error renaming"); return 1; }
 
    std::cout << std::ifstream("to.txt").rdbuf() << '\n'; // print file
}二次
产出:
二次
a二次
另见
rename (C++17)  | moves or renames a file or directory (function)  | 
|---|---|
remove  | erases a file (function)  | 
C重命名文件
 © cppreference.com在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com

