C++修改文件名

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012750702/article/details/53326521

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;
}



猜你喜欢

转载自blog.csdn.net/u012750702/article/details/53326521