Visual C++ File I/O

Hi everyone, I've been trying to figure out how to make, for example a button in Visual Studio C++ that can take some text from a textbox and save it as a file.

But I don't mean using SaveFileDialog, I want to have it where when the user presses the button it saves the file to a pre-programmed location, and the user wouldn't be able to see anything happen (apart from the file appearing in the location of course). I've tried to use #include <fstream>, but it just generates some errors (not compiling the program).

Here is my attempted code:

#include <fstream>

button_clicked()
{
String^ str = textboxX->Text;

ofstream file(file.txt);
file<<str;
}

This isn't all the code of the program, but these are the bits i have added to make it save text from a textbox to a text file. Any suggestions and advice would be appreciated as it would really help me out. Thanks in advance.
The internal char array in System::String is a wide char.
1
2
3
4
5
6
7
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
				
				 pin_ptr<const wchar_t> p = PtrToStringChars(textBox1->Text);
				 std::wstring s(p);
				 std::wofstream file("file.txt");
				 file<<s;
			 }

If you need it to be std::string see this http://msdn.microsoft.com/en-us/library/1b4az623(v=vs.80).aspx
Topic archived. No new replies allowed.