Unicode richtextbox to text file

Jan 24, 2012 at 11:03am
Hi,

I'm having a problem with taking the user input from a richtextbox in Unicode and writing it to a Unicode text file.

I'm able to read a different Unicode file and write it to the new file, but when it comes to writing the contents of the richtextbox to the new file, nothing appears.

I've tried debugging it, and I can see that the wstring is assigned correctly, I just can't see why this won't write to file.

What am I doing wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
	std::wifstream inFile(L"testin.txt", std::ios::in | std::ios::binary);
	std::wofstream outFile(L"test.txt", std::ios::app | std::ios::binary);
	std::wstring my_string;
	while(getline(inFile,my_string))
	{
		outFile<<my_string + L"\x0a\x00";
	}

	const wchar_t* chars = (const wchar_t*)(Marshal::StringToHGlobalUni(richTextBox1->Text)).ToPointer();
	std::wstring str1(chars);
	outFile<<str1 + L"\x0a\x00";
	outFile.close();
}


Thanks in advance!
Jan 24, 2012 at 1:46pm
You are using .Net. Why use regular C++? It is far easier to just use System::IO::FileStream and a StreamWriter object to write the text. No marshaling required if you stay in the confines of .net.
Jan 24, 2012 at 3:48pm
Hi, thanks for the quick reply. I've changed my code now so that it lines up with what you suggested and it works great! Thank you.

1
2
3
4
System::IO::StreamWriter^ swFromFileTrueUTF16 = gcnew System::IO::StreamWriter(L"test.txt",true, System::Text::Encoding::Unicode);
	swFromFileTrueUTF16->Write(richTextBox1->Text + L"\x0d\x0a");
	swFromFileTrueUTF16->Flush();
	swFromFileTrueUTF16->Close();
Topic archived. No new replies allowed.