C++:Boost 文件系统 copy_file 出错

标签 c++ boost boost-filesystem

我在使用 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 and Path2::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> if from_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/

相关文章:

c++ - 将柯南包含链接到VS代码

C++模板检查值

C++ directory_iterator

c++ - undefined symbol boost::system:detail::system_category_instance

c++ - 无法连接 qml 信号和 c++ 插槽

c++ - 如何使用宏#define 更改类名?

python - 动态数组和删除运算符的 boost::python 内存错误

c++ - 嵌入 Python : How to help the scripter?

c++ - 如何使用 Boost 扩展 .ini 文件中的环境变量

c++ - 为什么 std::filesystem::path::compare() 和 boost::filesystem::path::compare() 不同?