简体   繁体   English

C ++:Boost Filesystem copy_file错误

[英]C++: Error with Boost Filesystem copy_file

I'm running into some trouble with the copy_file function. 我在使用copy_file函数时遇到了一些麻烦。 My program is very simple, I'm just attempting to copy a text file from one spot to another. 我的程序非常简单,我只是尝试将文本文件从一个位置复制到另一个位置。

The following code brings up a "Debug Error!" 以下代码显示“调试错误!” because abort() was called. 因为调用了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;
}

If I look at some other examples of code on Stackoverflow I cannot notice what I'm doing wrong. 如果我看一下Stackoverflow上的其他一些代码示例,则不会注意到我做错了什么。 I feel like I'm missing something obvious here. 我觉得这里缺少明显的东西。

I have Boost v1.47 installed and I'm using Visual C++ 2010. 我已经安装了Boost v1.47,并且正在使用Visual C ++ 2010。

I'm guessing that the target file exists. 我猜目标文件存在。

The 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. 要求Path1::external_string_typePath2::external_string_type是同一类型。

Effects : The contents and attributes of the file from_fp resolves to are copied to the file to_fp resolves to. 效果 :将from_fp解析为的文件的内容和属性复制到to_fp解析为的文件。 Throws : basic_filesystem_error<Path> if from_fp.empty() || 抛出basic_filesystem_error<Path>如果from_fp.empty() || to_fp.empty() || to_fp.empty() || !exists(from_fp) || !exists(from_fp) || !is_regular_file(from_fp) || !is_regular_file(from_fp) || exists(to_fp)

A simple test like so: 像这样的简单测试:

#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;
}

Prints: 印刷品:

src exists = true
Error: boost::filesystem::copy_file: File exists: "test.in", "test.out"

on the second run :) 在第二次运行:)

I think if you are using boost::filesystem2 it should be boost::filesystem2::copy(src_path,dest_path); 我认为如果您使用boost :: filesystem2,则应该使用boost :: filesystem2 :: copy(src_path,dest_path); copy_file should have been deprecated. copy_file应该已被弃用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM