StreamWriter output coming out as 'true'

Hi,

I'm having a problem with using the streamWriter to write to a unicode file.
Code as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
for(int i = 1; i < 1018; ++i)
{
	std::wostringstream integer;
	integer << L"IDS_STRING" << i << L"\t";
	wstring thisString = integer.str();
	thisString = thisString + L"\"Spare String\"";
	wchar_t* SpareString = const_cast<wchar_t*>(thisString.c_str());
	bool isPresent = std::find_if(load.begin(), load.end(), StartsWith1(thisString)) != load.end();

	System::IO::StreamWriter^ WriteSpareStrings = gcnew System::IO::StreamWriter(L"Chinese.txt", true, System::Text::Encoding::Unicode);
	if(isPresent == false)
	{
		WriteSpareStrings->Write(SpareString);
		WriteSpareStrings->Write(L"\r\n");
	}
	WriteSpareStrings->Flush();
	WriteSpareStrings->Close();
}


So, obviously I'm expecting the output to the file to be strings of "IDS_STRINGi "Spare String"", but instead all the strings are just written "True".

I have a warning as follows:
'warning C4800: 'wchar_t *' : forcing value to bool 'true' or 'false' (performance warning)'

This obviously gives me an idea that it's coming from the line - WriteSpareStrings->Write(SpareString) and that it doesn't like wchar_t* being used there, but I don't know a way of fixing it.

I've tried writing each separate part alone, which works apart from writing the integer, where it just prints out strange characters (probably to do with using Unicode?).

If anyone has any insight on how I can fix this, I'd greatly appreciate it.

Thank you
Last edited on
I don't see the need of mixing unmanaged C++ with C++/CLI. The .Net framework has its own version of a stringstream called a StringBuilder. Use that and write the whole thing using .Net.

To be specific: Line 13 attempts to write an unmanaged C string using a managed object that expects a managed System.String object.

Also remember this: If you have to use const_cast<>, you are most likely doing things wrong. And line 7 is no exception. If you declare SpareString of type const wchar_t* you don't have the need to use const_cast<>.

EDIT: One more thing. Same problem in line 14. Instead, use WriteSpareString->Write(Environment::NewLine);, or change 13 to use WriteLine() instead of Write().
Last edited on
Thanks for the quick reply. I've had a quick look into StringBuilder and have just one question: can it create and write Unicode files (like SreamWriter can specify the encoding)?
The System.String class of .Net is 100% Unicode. Or better said: Al strings in .Net are Unicode strings.
Last edited on
Topic archived. No new replies allowed.