I'm writing a code that will save the username of a user when a button (in this case button1) gets pressed. I save it to a file called info.txt, and i use fstream in order to save it. Currently I'm running into the issue that, while i can open up the file fine, I can't save the data from
textBox1 because it uses the data type String ^, while fstream doesn't support writing in that type. Here is my relevant code:

    private: System::Void textBox1_TextChanged(System::Object^  sender, System::EventArgs^  e)
    {

    }
    private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
    {
        std::ofstream settings("settings.txt");
        settings >> textBox1->Text >> std::endl
    }

Obviously I can't use this because textBox1->Text returns the wrong type. Anybody have any solutions?

While I'd start by suggesting that you use the .NET file stream options instead, String^ can be converted to std::string using marshal_as if so needed.

commented: This worked, I posted the new code. BTW, I know you can use .NET file stream, but since I'm more acquainted with the fstream system, I used it. +1

Thanks to @deceptikon, I was able to use this code to successfully write the text from the text box to a text file:

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
    {
        std::ofstream settings("settings.txt");
        String ^ buttonText1 = textBox1->Text;
        std::string buttonText1std = msclr::interop::marshal_as<std::string>(buttonText1);
        settings << buttonText1std << std::endl;
        settings.close();
    }

Thank you for your help!

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.