如何解决使用C ++将文件从一个目录复制到另一个目录
我正在编写一个C ++程序,用于将一个文件从一个目录复制到另一个目录。我不想使用C ++ 17功能。我已经在以下代码中实现了这一点。
#include <iostream>
#include <exception>
#include <filesystem>
using std:: cout;
using std:: cin;
using std:: endl;
int main(int argc,char* argv[])
{
if(argc != 3) {
cout << "Usage: ./copyFile.out path_to_the_file destination_path";
return 1;
}
std:: string source = argv[1];
std:: string destination = argv[2];
std:: filesystem:: path sourceFile = source;
std:: filesystem:: path targetParent = destination;
auto target = targetParent / sourceFile.filename();
try
{
std:: filesystem:: create_directories(targetParent); // Recursively create the target directory path if it does not exist.
std:: filesystem:: copy_file(sourceFile,target,std ::filesystem ::copy_options::overwrite_existing);
}
catch (std::exception& e) //If any filesystem error
{
std::cout << e.what();
}
return EXIT_SUCCESS;
}
我在Linux上,并且我想使用OS cp
命令来执行此操作。我已经写了这段代码。
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc,char *argv[]) {
std:: string source,destination;
if(argc != 3) {
cout << "Usage: ./copyFile.out path_to_the_file destination_path";
return 1;
}
source = argv[1];
destination = argv[2];
system("cp source destination");
}
错误是:cp:源:没有这样的文件或目录,我该如何使用system()?
解决方法
更改此:
system("cp source destination");
对此:
std::string cmd = std::string("cp '") + source + "' '" + destination + "'";
system(cmd.c_str());
顺便说一句,您应该从if(argc != 3)
语句内部返回,或者在else
语句内部执行其余代码。
最后,函数int main(int argc,char *argv[])
要求您返回一个int
值。
这也可以使用const router = useRouter();
const car = router.query.data;
console.log(car)
snprintf
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。