C++: Error with Boost Filesystem copy_file

11,311

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.

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)

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 :)

Share:
11,311
akaGrim
Author by

akaGrim

Updated on June 04, 2022

Comments

  • akaGrim
    akaGrim almost 2 years

    I'm running into some trouble with the copy_file function. 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.

    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. I feel like I'm missing something obvious here.

    I have Boost v1.47 installed and I'm using Visual C++ 2010.