942,960 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 22330
  • C++ RSS
Nov 16th, 2007
0

C++ copy file

Expand Post »
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)

c++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ace_joker_bt is offline Offline
2 posts
since Nov 2007
Nov 16th, 2007
0

Re: C++ copy file

I really don't know what you mean, but you might want to open the files in binary mode...
Reputation Points: 453
Solved Threads: 57
Posting Virtuoso
twomers is offline Offline
1,873 posts
since May 2007
Nov 16th, 2007
0

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5591
Solved Threads: 2280
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,930 posts
since Aug 2005
Nov 16th, 2007
0

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).
c++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Nov 17th, 2007
0

Re: C++ copy file

Thanks for your help!!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ace_joker_bt is offline Offline
2 posts
since Nov 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: i need help......
Next Thread in C++ Forum Timeline: factorial





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC