std::rename
提供: cppreference.com
<tbody>
</tbody>
| ヘッダ <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 によって再現されます。
例
Run this code
#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;
}
if (std::rename("from.txt", "to.txt")) {
std::perror("Error renaming");
return 1;
}
std::cout << std::ifstream("to.txt").rdbuf() << '\n'; // print file
}
出力:
a
関連項目
(C++17) |
ファイルまたはディレクトリを移動または名前変更します (関数) |
| ファイルを消去します (関数) | |
rename の C言語リファレンス
| |