954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Unicode richtextbox input to text file

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?

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!

ahoysailor
Newbie Poster
14 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

Can you use a StreamWriter ?
Please read the statement about Unicode at that link.

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

Hi,

Thanks for the reply.

I looked up streamWriter as you suggested and have implemented it, and now it works perfectly, thanks!

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();
ahoysailor
Newbie Poster
14 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: