1. 程式人生 > >C++中修改資料夾名以及檔名

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的知識。