名稱空間
變體
操作

std::rename

來自 cppreference.com
< cpp‎ | io‎ | c
 
 
 
 
定義於標頭檔案 <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')) // create and write to file
    {
        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'; // print file
    return EXIT_SUCCESS;
}

輸出

a

[編輯] 另請參閱

(C++17)
移動或重新命名檔案或目錄
(函式) [編輯]
擦除檔案
(函式) [編輯]
C 文件 for rename