Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to save text from an Edit field into a *.txt file

Status
Not open for further replies.

TheAwake

Programmer
Jul 28, 2003
37
I have a question about C++, I have a button1 and a edit1 and I want that when the button is pressed, the text that has been written into the edit1 is saved into a *.txt file (which should be in the same directory like the *.exe file).

But I also want that I can close the programm and open it later again, and the new text is added to the *.txt file, not that the file is overwritten.
Have you any ideas how that works?
 
Pretty simple. Everytime you load the program, create a TStringList and load the text from the file into that list. Next, when someone presses the button, add that string to the end of the TStringList and then save that list to file when exiting the program. You can doctor that up and make it check for duplicate strings, etc. pretty easy.

Good luck!
Chris
 
A way is as follows:
Code:
  #include <fstream.h>

  char myDummy[100];

  ofstream outfile(&quot;C:\\Copy\\test.txt&quot;, ios::app);
  // Opens file in an append mode
  outfile << Edit1->Text.c_str() << endl;
  // c_str() changes Edit1->Text to char.
  outfile.close();
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top