文章目录
调用filesystem库的rename()方法,用于移动或重命名文件。这里用其移动的功能。
仅仅把一个文件移动另一个文件夹,名称不变
#include <filesystem> // 对文件路径名的操作
// 第一个参数包含文件名(./a.txt),第二个参数包含地址(./new/),文件名保留原文件名
bool radar_analysis::move_file(std::string old_path_and_file_name, std::string new_path)
{
std::filesystem::path file_old_path(old_path_and_file_name);
string file_name = file_old_path.filename().string();
std::string resule_path = new_path + file_name;
std::error_code ec;
std::filesystem::rename(old_path_and_file_name, resule_path, ec);
if (ec)
{
return false;
}
else
{
return true;
}
}
int main()
{
std::string old_path = "./a.txt";
std::string new_path = "../code/";
move_file(old_path , new_path );
}