appending the end of a file

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Apr 2008
Posts: 34
Reputation: Seamus McCarthy is an unknown quantity at this point 
Solved Threads: 0
Seamus McCarthy Seamus McCarthy is offline Offline
Light Poster

appending the end of a file

 
0
  #1
Oct 29th, 2008
I'm trying to append the end of the file treatments.txt but am unsure how to use the while loop correctly!! Any suggestions?? I tried boolean with the whiles aswell but can't seem to get it to work either. Like asking do you want to continue adding treatments and if yes set bool to false and exit while statement.


  1. void enterTreatments( T * treatArray, int numTreatments)
  2. {
  3. int treatmentNumber = -1;
  4.  
  5. ofstream outfile;
  6. outfile.open("treatments.txt");
  7.  
  8. // while(numTreatments > treatmentNumber)
  9. for (int i = 0; i < numTreatments; i++)
  10. {
  11. cout << "Enter treatment ID" << endl;
  12. cin >> treatArray[i].treatmentID;
  13. outfile << treatArray[i].treatmentID;
  14. cin.get();
  15.  
  16. cout << "Enter treatment Description" << endl;
  17. getline(cin,treatArray[i].treatmentDescription);
  18. outfile << treatArray[i].treatmentDescription;
  19.  
  20. cout << "Enter cost for treatment" << endl;
  21. cin >> treatArray[i].cost;
  22. outfile << treatArray[i].cost;
  23.  
  24. cout << "Enter number of required sessions" << endl;
  25. cin >> treatArray[i].noOfRequiredSessions;
  26. outfile << treatArray[i].noOfRequiredSessions;
  27.  
  28. treatArray[i].costPerSession = treatArray[i].cost/treatArray[i].noOfRequiredSessions;
  29.  
  30. //cout << "Enter cost per session " << endl;
  31. //cin >> treatArray[i].costPerSession;
  32. cout << "Cost for each session is = " ;
  33. cout << treatArray[i].costPerSession << endl;
  34. cin.get();
  35.  
  36. outfile << treatArray[i].costPerSession;
  37. treatmentNumber++;
  38.  
  39. }
  40.  
  41. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,861
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 755
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: appending the end of a file

 
1
  #2
Oct 29th, 2008
>Any suggestions??
Open the file with the ios::app mode, then you don't have to worry about it. Every write will be an append.
New members chased away this month: 5
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 34
Reputation: Seamus McCarthy is an unknown quantity at this point 
Solved Threads: 0
Seamus McCarthy Seamus McCarthy is offline Offline
Light Poster

Re: appending the end of a file

 
0
  #3
Oct 29th, 2008
Ohh get you now. Thanks!!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 1027 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC