CString insertion operator faulty?
-
Hi I am trying to print a CString to a file using C++ ofstream. What I do is something like this: CString cstr = "This is a test"; std::ofstream fTest("TestCString.txt"); fTest << cstr; fTest.close(); The problem is, the result in TestCString.txt is not the text "This is a test", instead it is something like 002F4BC4, which I couldn't make any sense out of it. Could someone tell me what the problem is, and since the string in my project MUST use CString, is there a way to print it using ofstream without changing the CString class? Thanks!
-
Hi I am trying to print a CString to a file using C++ ofstream. What I do is something like this: CString cstr = "This is a test"; std::ofstream fTest("TestCString.txt"); fTest << cstr; fTest.close(); The problem is, the result in TestCString.txt is not the text "This is a test", instead it is something like 002F4BC4, which I couldn't make any sense out of it. Could someone tell me what the problem is, and since the string in my project MUST use CString, is there a way to print it using ofstream without changing the CString class? Thanks!
-
Hi I am trying to print a CString to a file using C++ ofstream. What I do is something like this: CString cstr = "This is a test"; std::ofstream fTest("TestCString.txt"); fTest << cstr; fTest.close(); The problem is, the result in TestCString.txt is not the text "This is a test", instead it is something like 002F4BC4, which I couldn't make any sense out of it. Could someone tell me what the problem is, and since the string in my project MUST use CString, is there a way to print it using ofstream without changing the CString class? Thanks!
-
Hi I am trying to print a CString to a file using C++ ofstream. What I do is something like this: CString cstr = "This is a test"; std::ofstream fTest("TestCString.txt"); fTest << cstr; fTest.close(); The problem is, the result in TestCString.txt is not the text "This is a test", instead it is something like 002F4BC4, which I couldn't make any sense out of it. Could someone tell me what the problem is, and since the string in my project MUST use CString, is there a way to print it using ofstream without changing the CString class? Thanks!
When you typecast object of CString with LPCTSTR, internally overloaded operator LPCTSTR will get called. else explicily call function of CString class "operator LPCTSTR()" This will convert CString object to Long Pointer To Constant String which is acceptable format for ofstream. The chosen One :)
-
Hi I am trying to print a CString to a file using C++ ofstream. What I do is something like this: CString cstr = "This is a test"; std::ofstream fTest("TestCString.txt"); fTest << cstr; fTest.close(); The problem is, the result in TestCString.txt is not the text "This is a test", instead it is something like 002F4BC4, which I couldn't make any sense out of it. Could someone tell me what the problem is, and since the string in my project MUST use CString, is there a way to print it using ofstream without changing the CString class? Thanks!
Hmmm, I tried your exact code (without the suggested cast) and it wrote "This is a test" to the file.
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
-
Hmmm, I tried your exact code (without the suggested cast) and it wrote "This is a test" to the file.
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
You're using VS.NET 2002 or 2003, while the OP is using VC6. Something in either the standard library or in CString has changed enough to fix this issue between the two. The changes to CString in MFC 7.x are extensive: it's now defined in an ATL header, for a start. It's also a template class: CString is a typedef for CStringT< StrTraitMFC< TCHAR, ChTraitsCRT< TCHAR > > >. Stability. What an interesting concept. -- Chris Maunder
-
You're using VS.NET 2002 or 2003, while the OP is using VC6. Something in either the standard library or in CString has changed enough to fix this issue between the two. The changes to CString in MFC 7.x are extensive: it's now defined in an ATL header, for a start. It's also a template class: CString is a typedef for CStringT< StrTraitMFC< TCHAR, ChTraitsCRT< TCHAR > > >. Stability. What an interesting concept. -- Chris Maunder
-
I copied the OP's code into a new Win32 Console project in VC6 (selecting 'An application that supports MFC' from the Wizard). It produced the original output. Opening the project in VS.NET 2003, converting it and rebuilding produced the correct output. The fault is that the compiler appears to be selecting the class member (of
basic_ostream
)operator<<(const void*)
for some reason. It's definitely callingCString::operator LPCTSTR
to get the pointer whether you include the explicit cast or not. Have you upgraded to Visual Studio 6.0 Service Pack 6, and could this be a solution to the problem? Stability. What an interesting concept. -- Chris Maunder -
I copied the OP's code into a new Win32 Console project in VC6 (selecting 'An application that supports MFC' from the Wizard). It produced the original output. Opening the project in VS.NET 2003, converting it and rebuilding produced the correct output. The fault is that the compiler appears to be selecting the class member (of
basic_ostream
)operator<<(const void*)
for some reason. It's definitely callingCString::operator LPCTSTR
to get the pointer whether you include the explicit cast or not. Have you upgraded to Visual Studio 6.0 Service Pack 6, and could this be a solution to the problem? Stability. What an interesting concept. -- Chris MaunderMike Dimmick wrote: Have you upgraded to Visual Studio 6.0 Service Pack 6... Yes, I have SP6.
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
-
Hi I am trying to print a CString to a file using C++ ofstream. What I do is something like this: CString cstr = "This is a test"; std::ofstream fTest("TestCString.txt"); fTest << cstr; fTest.close(); The problem is, the result in TestCString.txt is not the text "This is a test", instead it is something like 002F4BC4, which I couldn't make any sense out of it. Could someone tell me what the problem is, and since the string in my project MUST use CString, is there a way to print it using ofstream without changing the CString class? Thanks!
You have to cast the CString to an LPCTSTR ala
fTest << (LPCTSTR)cstr;
So that the insertion operator uses the right overload. If you're using unicode, (e.g., L"This is a test" or, _T("This is a test") and unicode is turned on), then you'll want to use a std::wofstream instead of std::ofstream. But you'll still need to cast cstr to LPCTSTR. If you, like me, tried testing this with cout, try using wcout instead (the cast to LPCTSTR is still required).