C++中修改文件夹名以及文件名

我们可以通过程序来修改已经存在的文件夹或是文件的命名。

之前没有尝试过,因为需要一些复杂的操作。

但是殊不知,仅仅一个rename函数就可以搞定,自惭形秽。

直接上代码吧:

#include <iostream>
#include <fstream>
#include<Windows.h>

int WINAPI WinMain(HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
    int nCmdShow)
{
    std::string original_name = "D:\\originalname";
    std::string new_name= "D:\\newname";
    std::fstream f;
    f.open(original_name.c_str());
    if (f)
    {
        rename(original_name.c_str(), new_name.c_str());
        MessageBox(NULL, TEXT("RENAME SUCCESS"), NULL, NULL);
        f.close();
    }
    else
    {
        MessageBox(NULL, TEXT("NO FILE"), NULL, NULL);
        f.close();

    }

    return 0;
}

简单极了。主要就是设计到一些IO的知识。

你可能感兴趣的:(C++,rename)