0

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?

2
Contributors
2
Replies
23
Views
3 Weeks
Discussion Span
Last Post by Nether_1
1

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.

Votes + Comments
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!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.