944,110 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 3625
  • C++ RSS
Oct 17th, 2007
0

how to append input at the end of the old data in a text file?

Expand Post »
Hello there, I need to write a function that insert new data into a text file, and I manage to do that, but only for the first row....
When I close my program, and reopen it to key in a 2nd input, it will simplely overwrite my 1st input.
how can I insert the 2nd input at the next row of the 1st input?

here is my code:
C++ Syntax (Toggle Plain Text)
  1.  
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. ofstream fout;
  11. string input;
  12. string title,author,category;
  13. float price;
  14.  
  15. fout.open("library.txt");
  16. if (!fout.good())
  17. {
  18. cerr << "Error creating file";
  19. }
  20.  
  21. cout << "Title: " ;
  22. getline(cin, input);
  23. title = input;
  24.  
  25. cout << "Author: " ;
  26. getline(cin, input);
  27. author = input;
  28.  
  29. cout << "Category: ";
  30. getline(cin, input);
  31. category = input;
  32.  
  33. cout << "Price: " ;
  34. getline(cin, input);
  35. istringstream strPrice(input);
  36. strPrice >> price;
  37.  
  38. fout<< title << '\t' << author << '\t' << category << '\t' << price << "\n";
  39. fout.close();
  40. }

Many thanks~
Last edited by toncoolx; Oct 18th, 2007 at 12:05 am.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
toncoolx is offline Offline
10 posts
since Oct 2007
Oct 17th, 2007
0

Re: how to append input at the end of the old data in a text file?

A couple ways to solve your problem:

1. after opening the file call seekp(ios::end); set the file pointer to end-of-file before writing

2. Add the ios::ate flag to the open statement: fout.open("library.txt", ios::out | ios::ate);
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
Oct 18th, 2007
0

Re: how to append input at the end of the old data in a text file?

Hi, Ancient Dragon, I tried both of your codes, but it still replace the 1st row's data, is it my codes got problems?

how i follow your code is like:
C++ Syntax (Toggle Plain Text)
  1. fout.open("library.txt");
  2. fout.seekp(ios::end);
  3. if (!fout.good())
  4. .
  5. .
  6. .

2nd one:
C++ Syntax (Toggle Plain Text)
  1. fout.open("library.txt", ios::out | ios::ate);
  2. if (!fout.good())
  3. .
  4. .
  5. .


thanks~
Reputation Points: 10
Solved Threads: 0
Newbie Poster
toncoolx is offline Offline
10 posts
since Oct 2007
Oct 18th, 2007
0

Re: how to append input at the end of the old data in a text file?

try this:
C++ Syntax (Toggle Plain Text)
  1. fout.open("library.txt");
  2. if( fout.is_open())
  3. {
  4. fout.seekp(0,ios_base::end);
  5. // blabla
  6. }

If that doesn't work then post how fout was declared.
Last edited by Ancient Dragon; Oct 18th, 2007 at 12:27 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
Oct 18th, 2007
0

Re: how to append input at the end of the old data in a text file?

i found the problem, when everytime i reopen the program, my library.txt will be empty(0 byte), that is why it will only display the last inputs.

is it
C++ Syntax (Toggle Plain Text)
  1. fout.open("library.txt");
create a new "library.txt", but not open "library.txt"?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
toncoolx is offline Offline
10 posts
since Oct 2007
Oct 18th, 2007
0

Re: how to append input at the end of the old data in a text file?

try
fout.open("Library.txt", ios::app);
i use it n it works....
Last edited by tracethepath; Oct 18th, 2007 at 3:37 am.
Reputation Points: 8
Solved Threads: 4
Junior Poster in Training
tracethepath is offline Offline
54 posts
since Jul 2007
Oct 18th, 2007
0

Re: how to append input at the end of the old data in a text file?

try
fout.open("Library.txt", ios::app);
i use it n it works....

Thanks, it works!!!!
hahahah, thanks a lot~
Reputation Points: 10
Solved Threads: 0
Newbie Poster
toncoolx is offline Offline
10 posts
since Oct 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: C++: Exception: Out of order execution?
Next Thread in C++ Forum Timeline: Is my if statement a valid statement?





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


Follow us on Twitter


© 2011 DaniWeb® LLC