注册 登录
编程论坛 C++教室

C++如何改名字

chaw899 发布于 2019-06-18 19:54, 1492 次点击
int main(int argc, char * argv[])
{
    string PATH = ".";
    vector<string> files;
    ifstream inF;                                    // 增加
    ofstream ouF;                                    // 增加
    ouF.open("nas", std::ofstream::binary);          // 增加
    readFileList(PATH, files, "ts");
    cout << "Please wait!" << endl;                  // 增加
    for (int i = 0; i < files.size(); ++i)
    {
        inF.open(files[i], std::ofstream::binary);   // 增加
        ouF << inF.rdbuf();                          // 增加
        inF.close();                                 // 增加   
        // cout << files[i] << endl;                 // 这里可以删除
    }
    ouF.close();                                     // 增加
    return 0;
}

程序退出的时候把自己建立的nas改名为nas.ts,这个如何实现啊?
1 回复
#2
rjsp2019-06-19 08:45
那你一开始为什么不将之直接命名为 nas.ts ?

C++ 标准库中支持文件重命名的是 std::filesystem::rename,参加:https://en.

或者你可以使用系统APIs,比如 windows 上的 MoveFile 函数,POSIX 上的 rename 函数。
1