I really don't know what you mean, but you might want to open the files in binary mode...
twomers
Posting Virtuoso
1,877 posts since May 2007
Reputation Points: 453
Solved Threads: 57
If you are trying to write that code for MS-Windows then you can use win32 api CopyFile() function. Otherwise open the input and output files in binary mode, not the default text mode. On *nix it doesn't matter because they are both the same, but on MS-Windows there is a difference.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
the easiest, least error-prone and the fastest way to copy an entire stream is this:
output_stream << input_stream.rdbuf() ;. if you are writing a utility like this, error messages are conventionally sent to cerr (not cout).
#include <iostream>
#include <fstream>
using namespace std ;
int main( int argc, char** argv )
{
if( argc != 3 )
{
cout << "usage: " << argv[0] << "<srce file> <dest file>" ;
return 1 ;
}
ifstream srce( argv[1], ios::binary ) ;
if( !srce )
{
cerr << "could not open " << argv[1] << " for reading\n" ;
return 2 ;
}
ofstream dest( argv[2], ios::binary ) ;
if( !dest )
{
cerr << "could not open " << argv[2] << " for writing\n" ;
return 3 ;
}
dest << srce.rdbuf() ;
if( !dest )
{
cerr << "error while copying\n" ;
return 4 ;
}
}
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287