C++ copy file

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2007
Posts: 2
Reputation: ace_joker_bt is an unknown quantity at this point 
Solved Threads: 0
ace_joker_bt ace_joker_bt is offline Offline
Newbie Poster

C++ copy file

 
0
  #1
Nov 16th, 2007
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)

  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4. int main ()
  5. {
  6. char data[50];
  7.  
  8. char inputFile [100];
  9. cout <<"Insert inputFilePatch :"<<endl;
  10. cin >> inputFile;
  11.  
  12. ifstream inputFilePatch;
  13. inputFilePatch.open(inputFile);
  14.  
  15. if(!inputFilePatch) {
  16. cout << "Wrong Location!!!\n";
  17. system("PAUSE");
  18. return 1;
  19. }
  20. char outputFile[100];
  21. cout<<"Insert outputFilePatch "<<endl;
  22. cin >> outputFile;
  23.  
  24. ofstream outputFilePatch;
  25. outputFilePatch.open(outputFile);
  26. outputFilePatch<<data;
  27.  
  28. while(inputFilePatch>>data){
  29. outputFilePatch<<data<< endl;
  30. };
  31.  
  32. outputFilePatch.close();
  33.  
  34. system("PAUSE");
  35. return 0;
  36.  
  37. }

This is exactly just for .txt files!
What to change to work these?
Last edited by Ancient Dragon; Nov 16th, 2007 at 6:28 pm. Reason: replaced icode tags with code tags.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 1,856
Reputation: twomers has a spectacular aura about twomers has a spectacular aura about twomers has a spectacular aura about 
Solved Threads: 53
twomers's Avatar
twomers twomers is online now Online
Posting Virtuoso

Re: C++ copy file

 
0
  #2
Nov 16th, 2007
I really don't know what you mean, but you might want to open the files in binary mode...
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,330
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1453
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: C++ copy file

 
0
  #3
Nov 16th, 2007
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: C++ copy file

 
0
  #4
Nov 16th, 2007
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).
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std ;
  4.  
  5. int main( int argc, char** argv )
  6. {
  7. if( argc != 3 )
  8. {
  9. cout << "usage: " << argv[0] << "<srce file> <dest file>" ;
  10. return 1 ;
  11. }
  12. ifstream srce( argv[1], ios::binary ) ;
  13. if( !srce )
  14. {
  15. cerr << "could not open " << argv[1] << " for reading\n" ;
  16. return 2 ;
  17. }
  18. ofstream dest( argv[2], ios::binary ) ;
  19. if( !dest )
  20. {
  21. cerr << "could not open " << argv[2] << " for writing\n" ;
  22. return 3 ;
  23. }
  24. dest << srce.rdbuf() ;
  25. if( !dest )
  26. {
  27. cerr << "error while copying\n" ;
  28. return 4 ;
  29. }
  30. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 2
Reputation: ace_joker_bt is an unknown quantity at this point 
Solved Threads: 0
ace_joker_bt ace_joker_bt is offline Offline
Newbie Poster

Re: C++ copy file

 
0
  #5
Nov 17th, 2007
Thanks for your help!!!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC