943,935 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 7617
  • C++ RSS
Oct 9th, 2006
0

Writing text from textbox to file...

Expand Post »
So I'm working on a program where you fill in some textboxes and press a button to generate a .txt file with the texts in it...

I'm working in Visual Studio Express (C++)

The only thing i have so far is:
private: System::Void btnAdd_Click(System::Object^ sender, System::EventArgs^ e)
{
char text1[20]
 
fstream file_op("C:\\Textol.txt", ios::out); 
file_op<<text1;
file_op.close();

and i included
#include<fstream>
#include<conio.h>
#include<stdio.h>
 
// and added to namespaces 
 
usingnamespace std;

The textboxes are auto generated etc...

The problem is How to I write the text from the textboxes into my file.. i cant add them like this:

C++ Syntax (Toggle Plain Text)
  1.  
  2. file_op<<txt1->Text;


I have to get some kind of way to save the string texts to a varable.. but how..
Similar Threads
DmD
Reputation Points: 10
Solved Threads: 0
Newbie Poster
DmD is offline Offline
9 posts
since Oct 2006
Oct 9th, 2006
0

Re: Writing text from textbox to file...

You know the ID of each text box, right? And you have the HWND of the dialog box that contains the edit controls, right? Then use GetWindowText() for each box
C++ Syntax (Toggle Plain Text)
  1. char text[255]; // or however long the strings are
  2. // hWnd is the HWND of the dialog box
  3. HWND hEditWnd = GetDlgItem(hWnd,IDC_EDIT1);
  4. // get 1st text box
  5. GetWindowText(hEditWnd, text);
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Oct 9th, 2006
0

Re: Writing text from textbox to file...

Since it appears you are using managed (.NET) code, why not use the .NET classes for file IO? I'm no expert at C++/CLI but it might be as simple as:
C++ Syntax (Toggle Plain Text)
  1. System::IO::StreamWriter^ sw = gcnew StreamWriter("file.txt");
  2. sw->WriteLine(txt1->Text);
Reputation Points: 108
Solved Threads: 14
Junior Poster in Training
GloriousEremite is offline Offline
65 posts
since Jul 2006
Oct 10th, 2006
0

Re: Writing text from textbox to file...

Since it appears you are using managed (.NET) code, why not use the .NET classes for file IO? I'm no expert at C++/CLI but it might be as simple as:
C++ Syntax (Toggle Plain Text)
  1. System::IO::StreamWriter^ sw = gcnew StreamWriter("file.txt");
  2. sw->WriteLine(txt1->Text);
Thanks allot that really helped :cheesy: :cheesy:
DmD
Reputation Points: 10
Solved Threads: 0
Newbie Poster
DmD is offline Offline
9 posts
since Oct 2006
Oct 10th, 2006
0

Re: Writing text from textbox to file...

OK so thanks for your help, onto the next problem!! :cheesy:

now that i wrote the text to the .txt file I want it (when the form loads) to read the text and display it in the textboxes....

what i got is this:
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e)
{
System::IO::StreamReader^ sr=gcnew StreamReader("C:\\Textol.txt");
sr->ReadLine();
sr->Close();
}

I'm jsut asking myself do I transfer the text in one action, or do i have to save it to a variable... (I don't have any idea where the text goes when I use sr->Readline(); )
DmD
Reputation Points: 10
Solved Threads: 0
Newbie Poster
DmD is offline Offline
9 posts
since Oct 2006
Oct 10th, 2006
0

Re: Writing text from textbox to file...

That's because it doesn't go anywhere
Try this:


C++ Syntax (Toggle Plain Text)
  1. private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e)
  2. {
  3. System::IO::StreamReader^ sr=gcnew StreamReader("C:\\Textol.txt");
  4. Txt1->Text = sr->ReadLine();
  5. sr->Close();
  6. }
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Oct 12th, 2006
0

Re: Writing text from textbox to file...

Thanks again that really helped, i guess im not that good yet at logical thinking *YET*
DmD
Reputation Points: 10
Solved Threads: 0
Newbie Poster
DmD is offline Offline
9 posts
since Oct 2006
Oct 12th, 2006
0

Re: Writing text from textbox to file...

Practice makes perfect ;-)
Last edited by Nick Evan; Mar 26th, 2010 at 9:40 am.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006

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: Iso Triangle
Next Thread in C++ Forum Timeline: C++





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


Follow us on Twitter


© 2011 DaniWeb® LLC