Copying a file

Hi,
I'd like to build a class which receives a file, and edits a copy of it.

How can the file's copy be generated neatly?

Should I build a Constructor which receives a Pointer to the File and makes a copy of it? (how is the Copying process done?)

Thank you.
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>
#include <fstream>

bool copy_stream( std::istream& in, std::ostream& out )
{ return out << in.rdbuf() && out ; }

bool copy_file( std::string srce_file_name, std::string dest_file_name )
{
    std::ifstream fin( srce_file_name, std::ios::binary ) ;
    std::ofstream fout(dest_file_name, std::ios::binary ) ;
    return copy_stream( fin, fout ) ;
}
Hi JLBorges

Thank you very much.

Could you explain please the instruction out << in.rdbuf() && out ; ?

At the heart of the implementation of a stream iis an associated stream buffer.
The stream buffer deals with the buffering and transportation of characters to or from the target or source device.

For more information: http://www.mr-edd.co.uk/blog/beginners_guide_streambuf


in.rdbuf() returns (a pointer to) the stream buffer associated with the stream in.
http://en.cppreference.com/w/cpp/io/basic_ios/rdbuf


out << in.rdbuf() extracts characters one by one from the input sequence controlled by the stream buffer and writes them into out till eof occurs on input, or the output fails.
Overload (8) in: http://en.cppreference.com/w/cpp/io/basic_ostream/operator_ltlt


There is no implicit conversion from stream to bool - explicit operator bool() const
http://en.cppreference.com/w/cpp/io/basic_ios/operator_bool

The && out part is just to provide a boolean context.

It could also have been written as: return bool( out << in.rdbuf() ) ;
or as: return !( out << in.rdbuf() ).fail() ;
Hi JLB
Thank you very much for your kind support and very helpful one!
Hi JLB,

I wonder, how could you integrate the above into a constructor?

i.e. have the constructor to receive a file, create a copy of it, and have the data member of the class to point on that file.

The thing is that the constructor does not return any value, so how could you know if the constructor succeeded or not to create a copy of the file?

would you use a in-out argument?

Thank you.
> The thing is that the constructor does not return any value,
> so how could you know if the constructor succeeded or not to create a copy of the file?

First of all there are things that just can’t be done right without exceptions. Consider an error detected in a constructor; how do you report the error? You throw an exception. That’s the basis of RAII (Resource Acquisition Is Initialization), which is the basis of some of the most effective modern C++ design techniques: A constructor’s job is to establish the invariants for the class (create the environment in which the member functions are to run) and that often requires the acquisition of resources, such as memory, locks, files, sockets, etc.
https://isocpp.org/wiki/faq/exceptions
Topic archived. No new replies allowed.