954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Writing text from textbox to file...

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:

file_op<<txt1->Text;



I have to get some kind of way to save the string texts to a varable.. but how..

DmD
Newbie Poster
9 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

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

char text[255]; // or however long the strings are
// hWnd is the HWND of the dialog box
HWND hEditWnd = GetDlgItem(hWnd,IDC_EDIT1);
// get 1st text box
GetWindowText(hEditWnd, text);
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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:

System::IO::StreamWriter^ sw = gcnew StreamWriter("file.txt");
sw->WriteLine(txt1->Text);
GloriousEremite
Junior Poster in Training
65 posts since Jul 2006
Reputation Points: 108
Solved Threads: 14
 

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:

System::IO::StreamWriter^ sw = gcnew StreamWriter("file.txt");
sw->WriteLine(txt1->Text);



Thanks allot that really helped :cheesy: :cheesy:

DmD
Newbie Poster
9 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

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
Newbie Poster
9 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

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

private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e)
{
System::IO::StreamReader^ sr=gcnew StreamReader("C:\\Textol.txt");
Txt1->Text = sr->ReadLine();
sr->Close();
}
Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

Thanks again that really helped, i guess im not that good yet at logical thinking *YET*

DmD
Newbie Poster
9 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

Practice makes perfect ;-)

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You