It works if I change the setting:
"Character Set = Not Set"
instead of UNICODE
this makes me nervous. Is this ok? Should I do something else?
Are CString still good to use in C++?
Thanks!
Main Topics
Browse All TopicsHope someone can help me!
I'm pulling my hair out (what little I have left!)
How can I write a CString to a text file?
I'm getting some funny numeric output.
ofstream::myfs
myfs.open("xxx");
CString mycs = CString("blahblah")
myfs << mycs;
myfs.close();
Emergency! Help!
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Well, actually I'd prefer std::string (http://www.sgi.com/tech/s
Another option you have is to use ofstream to open the file in binary mode, and use ofstream's write method http://www.cplusplus.com/r
This will work for CString featuring Unicode characters as well.
>>>> It works if I change the setting: "Character Set = Not Set" instead of UNICODE this makes me nervous. Is this ok?
If you set the MFC project to UNICODE the CString class turns to unicode as well, what means that the LPCTSTR type (what is the pointer type of the internal character array CString returns when using the cast jkr showed you above) actually is a 'const wchar_t *' where wchar_t is a 'short int', so it is a pointer to a short int. When streaming that to a ofstream, it prints the pointer value (what you called the funny numeric output).
Things get different, when you were using wofstream as jkr showed. Then, with UNICODE set all is fine as the wofstream can handle a 'wchar_t *' type. But actually the code goes wrong again, if you switch back from UNICODE to ANSI.
I personally would recommend to not using CString and STL streaming classes together. There is a CStringFile class which could be used instead of ofstream/wofstream in case you want to use CString. Or - might be better - you would go the way jkr prefers and use STL string classes only. In case you have to convert a CString you would need code like
CString str("xyz");
std::string s;
if (sizeof(TCHAR) == 1)
s = (const char*)str;
else
{
s = std::string('\0', str.GetLength()+1);
wcstombs(&s[0], (LPCWSTR)str, s.size());
}
to convert from either char or wchar_t.
Business Accounts
Answer for Membership
by: jkrPosted on 2008-08-30 at 13:05:21ID: 22353059
The simple soultion would be to access the character buffer directly using 'CString::operator LPCTSTR():
Select allOpen in new window