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

Dani AI

Generated

Nice thread. Since you are already in C++/CLI and using WinForms, you can skip native buffers and Win32 calls and lean on the .NET helpers that work directly with System::String^. and are on the right track. Two patterns that keep things simple and safe:

  • Do not hardcode C:\.... Pick a user folder and build the path.
  • Save one field per line (or a simple CSV) so you can map lines back to textboxes on load.

Example: write three textboxes, then read them back on form load.

using namespace System;
using namespace System::IO;

String^ path = Path::Combine(
    Environment::GetFolderPath(Environment::SpecialFolder::MyDocuments),
    "Textol.txt");

// Save: one value per line
File::WriteAllLines(path, gcnew array<String^> { txt1->Text, txt2->Text, txt3->Text });

// Load: map lines back safely
if (File::Exists(path)) {
    array<String^>^ lines = File::ReadAllLines(path);
    if (lines->Length > 0) txt1->Text = lines[0];
    if (lines->Length > 1) txt2->Text = lines[1];
    if (lines->Length > 2) txt3->Text = lines[2];
}

If you instead have a single multiline box, you can round-trip it in one line: textBoxMulti->Lines = File::ReadAllLines(path);

Prefer this over fstream in managed projects. If you must stick with native I/O as suggested, convert String^ to std::string first (e.g., msclr::interop::marshal_as<std::string>(txt1->Text)) before writing to a native stream.

Tip: wrap save/load in try { ... } catch (System::Exception^ ex) { MessageBox::Show(ex->Message); } so a missing or locked file does not crash your app. Also consider SaveFileDialog/OpenFileDialog so users choose where the file lives.

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.