名前空間
変種
操作

std::rename

提供: cppreference.com
< cpp‎ | io‎ | c
 
 
 
 
ヘッダ <cstdio> で定義
int rename(constchar*old_filename, constchar*new_filename );

ファイルのファイル名を変更します。 ファイルは old_filename の指す文字列によって表されます。 新しいファイル名は new_filename の指す文字列によって表されます。

new_filename が存在する場合、動作は処理系定義です。

目次

[編集]引数

old_filename - 名前変更するファイルを表すパスを格納しているヌル終端文字列を指すポインタ
new_filename - ファイルの新しいパスを格納しているヌル終端文字列を指すポインタ

[編集]戻り値

成功した場合は 0、エラーが発生した場合は非ゼロの値。

[編集]ノート

POSIX はこの関数の意味論について多数の追加の詳細を規定しています。 これは C++ でも std::filesystem::rename によって再現されます。

[編集]

#include <iostream>#include <fstream>#include <cstdio>int main(){bool ok{std::ofstream("from.txt").put('a')};// create and write to fileif(!ok){std::perror("Error creating from.txt");return1;}   if(std::rename("from.txt", "to.txt")){std::perror("Error renaming");return1;}   std::cout<<std::ifstream("to.txt").rdbuf()<<'\n';// print file}

出力:

a

[編集]関連項目

(C++17)
ファイルまたはディレクトリを移動または名前変更します
(関数)[edit]
ファイルを消去します
(関数)[edit]
close