红联Linux门户
Linux帮助

C++修改文件名

发布时间:2017-01-02 10:03:24来源:linux网站作者:Ladd7
windows 及 ubuntu下均验证成功。
 
很容易,一个函数就搞定了:
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\\new.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;  
}
 
本文永久更新地址:http://www.linuxdiyf.com/linux/27445.html