Question

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.

Was it helpful?

Solution

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

OTHER TIPS

I think if you are using boost::filesystem2 it should be boost::filesystem2::copy(src_path,dest_path); copy_file should have been deprecated.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top