| | |
how to append input at the end of the old data in a text file?
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Oct 2007
Posts: 10
Reputation:
Solved Threads: 0
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:
Many thanks~
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)
#include <iostream> #include <fstream> #include <string> using namespace std; int main() { ofstream fout; string input; string title,author,category; float price; fout.open("library.txt"); if (!fout.good()) { cerr << "Error creating file"; } cout << "Title: " ; getline(cin, input); title = input; cout << "Author: " ; getline(cin, input); author = input; cout << "Category: "; getline(cin, input); category = input; cout << "Price: " ; getline(cin, input); istringstream strPrice(input); strPrice >> price; fout<< title << '\t' << author << '\t' << category << '\t' << price << "\n"; fout.close(); }
Many thanks~
Last edited by toncoolx; Oct 18th, 2007 at 12:05 am.
A couple ways to solve your problem:
1. after opening the file call
2. Add the ios::ate flag to the open statement:
1. after opening the file call
seekp(ios::end); set the file pointer to end-of-file before writing2. Add the ios::ate flag to the open statement:
fout.open("library.txt", ios::out | ios::ate); 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.
•
•
Join Date: Oct 2007
Posts: 10
Reputation:
Solved Threads: 0
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:
2nd one:
thanks~
how i follow your code is like:
C++ Syntax (Toggle Plain Text)
fout.open("library.txt"); fout.seekp(ios::end); if (!fout.good()) . . .
2nd one:
C++ Syntax (Toggle Plain Text)
fout.open("library.txt", ios::out | ios::ate); if (!fout.good()) . . .
thanks~
try this:
If that doesn't work then post how fout was declared.
C++ Syntax (Toggle Plain Text)
fout.open("library.txt"); if( fout.is_open()) { fout.seekp(0,ios_base::end); // blabla }
If that doesn't work then post how fout was declared.
Last edited by Ancient Dragon; Oct 18th, 2007 at 12:27 am.
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.
•
•
Join Date: Oct 2007
Posts: 10
Reputation:
Solved Threads: 0
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 create a new "library.txt", but not open "library.txt"?
is it
C++ Syntax (Toggle Plain Text)
fout.open("library.txt");
![]() |
Similar Threads
- How separate data in Text file (C++)
- C++ Reading from a text file (C++)
- add a 0 at end of data in a text file (Shell Scripting)
- getting data from a text file and putting it in an excel file using visual basic 6.0 (Visual Basic 4 / 5 / 6)
- Read comma separated data from a text file (C)
- Inputting text file data into an array, please help! (C++)
- Help Reading Info in Text File Into an Array (C++)
- How to send text to a text file using j2me through http connection? (Java)
- Output in Text file-How to apply fprintf()? (C)
Other Threads in the C++ Forum
- Previous Thread: C++: Exception: Out of order execution?
- Next Thread: Is my if statement a valid statement?
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets







