DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   C++ copy file (http://www.daniweb.com/forums/thread97080.html)

ace_joker_bt Nov 16th, 2007 5:01 pm
C++ copy file
 
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?

twomers Nov 16th, 2007 5:20 pm
Re: C++ copy file
 
I really don't know what you mean, but you might want to open the files in binary mode...

Ancient Dragon Nov 16th, 2007 6:37 pm
Re: C++ copy file
 
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.

vijayan121 Nov 16th, 2007 11:21 pm
Re: C++ copy file
 
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 ;
  }
}

ace_joker_bt Nov 17th, 2007 4:42 am
Re: C++ copy file
 
Thanks for your help!!!


All times are GMT -4. The time now is 5:39 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC