我在使用 copy_file 函数时遇到了一些问题。我的程序非常简单,我只是试图将一个文本文件从一个地方复制到另一个地方。
以下代码会出现“调试错误!”因为调用了 abort()。
int main()
{
path src_path = "C:\\src.txt";
path dst_path = "C:\\dst.txt";
cout << "src exists = " << exists( src_path ) << endl; // Prints True
boost::filesystem::copy_file( src_path, dst_path );
return 0;
}
如果我查看 Stackoverflow 上的一些其他代码示例,我无法注意到我做错了什么。我觉得我在这里遗漏了一些明显的东西。
我安装了 Boost v1.47,我使用的是 Visual C++ 2010。
最佳答案
我猜目标文件存在。
docs :
template <class Path1, class Path2> void copy_file(const Path1& from_fp, const Path2& to_fp);
Requires:
Path1::external_string_type
andPath2::external_string_type
are the same type.Effects: The contents and attributes of the file from_fp resolves to are copied to the file to_fp resolves to. Throws:
basic_filesystem_error<Path>
iffrom_fp.empty()
||
to_fp.empty()
||
!exists(from_fp)
||
!is_regular_file(from_fp)
||
exists(to_fp)
像这样的简单测试:
#include <iostream>
#include <boost/filesystem.hpp>
int main()
{
using namespace boost::filesystem;
path src_path = "test.in";
path dst_path = "test.out";
std::cout << "src exists = " << std::boolalpha << exists( src_path ) << std::endl; // Prints true
try
{
boost::filesystem::copy_file( src_path, dst_path );
} catch (const boost::filesystem::filesystem_error& e)
{
std::cerr << "Error: " << e.what() << std::endl;
}
return 0;
}
打印:
src exists = true
Error: boost::filesystem::copy_file: File exists: "test.in", "test.out"
第二次运行:)
关于C++:Boost 文件系统 copy_file 出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8289874/