C++ファイル名の変更
776 ワード
Windowsおよびubuntuで検証に成功
簡単に1つの関数で終わります
この関数には、0が成功、1が失敗の戻り値があります.
簡単に1つの関数で終わります
rename(oldName.c_str(), newName.c_str())
この関数には、0が成功、1が失敗の戻り値があります.
#include <iostream>
#include <string>
#include <cstdlib>
int main(int argc, char *argv[])
{
std::string oldName, newName;
#ifdef _WIN32
oldName = "F:\\data\\test\\old.jpg";
newName = "F:\\data\\test\
ew.jpg";
#else
oldName = "/media/myUbuntu/F/data/test/old.jpg";
newName = "/media/myUbuntu/F/data/test/new.jpg";
#endif
if (!rename(oldName.c_str(), newName.c_str()))
{
std::cout << "rename success "<< std::endl;
}
else
{
std::cout << "rename error "<< std::endl;
}
return 0;
}