C ++:Boost Filesystem copy_file出错

时间:2011-11-27 23:12:43

标签: 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。

2 个答案:

答案 0 :(得分:1)

我猜测目标文件存在。

docs

  

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

     

需要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"
第二轮

:)

答案 1 :(得分:-1)

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

相关问题