2

我在使用 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。

4

2 回答 2

1

我猜目标文件存在。

文档:_

template <class Path1, class Path2> void copy_file(const Path1& from_fp, const Path2& to_fp);

Requires :Path1::external_string_typePath2::external_string_type是同一类型。

效果:文件 from_fp 解析到的内容和属性被复制到 to_fp 解析到的文件中。 抛出basic_filesystem_error<Path>如果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"

在第二次运行:)

于 2011-11-27T23:35:43.740 回答
-1

我认为如果您使用的是 boost::filesystem2 它应该是 boost::filesystem2::copy(src_path,dest_path); copy_file 应该已被弃用。

于 2011-11-27T23:33:23.350 回答