I can make this program to copy diverse format wihout losts.

example:
C:\\animal.exe to D:\\animal.exe
(The File location write in locale)

#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
    char data[50];        
    
   char inputFile [100];      
    cout <<"Insert inputFilePatch :"<<endl; 
    cin >> inputFile;    
        
    ifstream inputFilePatch;           
    inputFilePatch.open(inputFile);
    
    if(!inputFilePatch) {   
    cout << "Wrong Location!!!\n";
     system("PAUSE");
    return 1;
    }	
    char outputFile[100];  
    cout<<"Insert outputFilePatch  "<<endl; 
    cin >> outputFile;
    
    ofstream outputFilePatch;
    outputFilePatch.open(outputFile);    
    outputFilePatch<<data; 
    
   while(inputFilePatch>>data){
        outputFilePatch<<data<< endl;
        }; 
        
    outputFilePatch.close();
   
    system("PAUSE");
   return 0; 
   
}

This is exactly just for .txt files!
What to change to work these?

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.

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 ;
  }
}
This article has been dead for over six months. Start a new discussion instead.