Copy from one file to another

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

Join Date: Nov 2007
Posts: 14
Reputation: ENG ISE student is an unknown quantity at this point 
Solved Threads: 0
ENG ISE student ENG ISE student is offline Offline
Newbie Poster

Copy from one file to another

 
0
  #1
Dec 20th, 2007
So I'm supposed to copy the word "computer" or "computers" from the text of one file and past the word each time to the new file. It compiles but it doesnt run right. Any input would be great.

  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstring>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. char filename, filename2, word[20]; //char to read the input and output files as well as word from the file
  10. ofstream fout;//output file
  11. ifstream fin;//input file
  12.  
  13. cout << "Enter you input file:";
  14. cin>>filename;//stores the input filename
  15.  
  16. fin.open("filename");//open input file
  17.  
  18. cout << "Enter your output filename";
  19. cin>>filename2;//stores output filename
  20.  
  21. do
  22. {
  23. if(strcmp("COMPUTER", word) || strcmp ("COMPUTERS", word))//looks for both computer and computers in file
  24. {
  25.  
  26. fout.open ("filename2");//open output file
  27. fout<<word;//prints the word in the output file
  28. }
  29.  
  30. }while(word);
  31.  
  32. fin.close();//closes input file
  33. fout.close();//closes output file
  34. return 0;
  35. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 176
Reputation: dubeyprateek is an unknown quantity at this point 
Solved Threads: 22
dubeyprateek's Avatar
dubeyprateek dubeyprateek is offline Offline
Junior Poster

Re: Copy from one file to another

 
0
  #2
Dec 20th, 2007
  1. do
  2. {
  3. if(strcmp("COMPUTER", word) || strcmp ("COMPUTERS", word))//looks for both computer and computers in file
  4. {
  5.  
  6. fout.open ("filename2");//open output file
  7. fout<<word;//prints the word in the output file
  8. }
  9. // read the word here from the input file in 'word'
  10. }while(word);//curently it is a bad loop

is not doing the job. You need to read word by word and then compare. Also there is no need re opening the output file fout.open ("filename2");//open output file .
You can find several examples in this forum on how to read a word from a file.

-Prateek
Last edited by Ancient Dragon; Dec 20th, 2007 at 6:21 pm. Reason: corrected icode tag syntax
I know I am. Therefore I am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 35
Reputation: yagiD is an unknown quantity at this point 
Solved Threads: 7
yagiD's Avatar
yagiD yagiD is offline Offline
Light Poster

Re: Copy from one file to another

 
0
  #3
Dec 20th, 2007
There are errors in reading from and writing to a file and do-while results in an infinite loop.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,398
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: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Copy from one file to another

 
0
  #4
Dec 20th, 2007
>> char filename, filename2, word[20];
You are attempting to tread char as if it were std::string. My suggestion is to get rid of those c-style character arrays and use the easier-to-use std::string instead
  1. std::string filename, filename2, word;
  2.  
  3. ...
  4. cout << "Enter your output filename";
  5. getline(filename2,cin);//allow filename to contain spaces
  6. ofstream out(filename2.c_str());
  7. while( fin >> word)
  8. {
  9. if( word == "COMPUTER" || word == "COMPUTERS")
  10. fout << word;
  11.  
  12. }
Last edited by Ancient Dragon; Dec 20th, 2007 at 6:28 pm.
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  
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