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..

Recommended Answers

All 7 Replies

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);

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);

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:

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(); )

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();
}

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

Practice makes perfect ;-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.