CString problem in visual C++ 2005
-
following code snippet works in VC++ 6 file = fopen((LPCTSTR)CSTRING_VARIABLE,"wb+") but in VC 2005 above code can not compile because of CString and casting problem. how I can convert above code to be compatible with visual C++ 2005. Regards Gut Mikh
-
following code snippet works in VC++ 6 file = fopen((LPCTSTR)CSTRING_VARIABLE,"wb+") but in VC 2005 above code can not compile because of CString and casting problem. how I can convert above code to be compatible with visual C++ 2005. Regards Gut Mikh
What's the actual error? Second, do you really need the explicit cast? What is CSTRING_VARIABLE? You should be able to use a CString without the cast there, unless there's a conflict with char/wchar_t types. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
What's the actual error? Second, do you really need the explicit cast? What is CSTRING_VARIABLE? You should be able to use a CString without the cast there, unless there's a conflict with char/wchar_t types. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
fopen function need one const char * as its first parameter. in visual C++ 6 we can cast a CString variable to a char* by using (LPCTSTR) . but this simple methodology does not work for visual C++ 2005. Because we want to upgrade one project from VC6 to VC2005, thus it is neccessary to change it. Now I want a simple code snippet which converts a CString variable to char* . Regards Gut Mikh.
-
fopen function need one const char * as its first parameter. in visual C++ 6 we can cast a CString variable to a char* by using (LPCTSTR) . but this simple methodology does not work for visual C++ 2005. Because we want to upgrade one project from VC6 to VC2005, thus it is neccessary to change it. Now I want a simple code snippet which converts a CString variable to char* . Regards Gut Mikh.
You shouldn't need the cast. CString has a cast operator that does this for you. I'm still wondering what the actual compiler error is.
Gut Mikh Tappe wrote:
we can cast a CString variable to a char* by using (LPCTSTR)
That's not a valid cast. LPCTSTR is a constant pointer to a TCHAR. TCHAR can be a char or a wchar_t, depending on if _UNICODE is defined. Without knowing the error message, I'm guessing your build settings are for unicode, in which case you should either change the project settings to not use unicode, use the correct CStringT type, or use _tfopen() instead of fopen(). Regardless, you should NOT need the cast. Casts should be used only when absolutely necessary, because they can hide errors, exactly as demonstrated here. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
fopen function need one const char * as its first parameter. in visual C++ 6 we can cast a CString variable to a char* by using (LPCTSTR) . but this simple methodology does not work for visual C++ 2005. Because we want to upgrade one project from VC6 to VC2005, thus it is neccessary to change it. Now I want a simple code snippet which converts a CString variable to char* . Regards Gut Mikh.
Try: LPCSTR Instead of LPCTSTR
-
You shouldn't need the cast. CString has a cast operator that does this for you. I'm still wondering what the actual compiler error is.
Gut Mikh Tappe wrote:
we can cast a CString variable to a char* by using (LPCTSTR)
That's not a valid cast. LPCTSTR is a constant pointer to a TCHAR. TCHAR can be a char or a wchar_t, depending on if _UNICODE is defined. Without knowing the error message, I'm guessing your build settings are for unicode, in which case you should either change the project settings to not use unicode, use the correct CStringT type, or use _tfopen() instead of fopen(). Regardless, you should NOT need the cast. Casts should be used only when absolutely necessary, because they can hide errors, exactly as demonstrated here. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
I do agree with Mark's comment. When you create a new project in Visual C++ 2005, probably you might have not noticed the "Use Unicode Libraries" in the Wizard. While you are dealing with these it's better to use tchar functions. You can get those functions just by including #include Please post the error code if possible.
-Sarath_._ "Great hopes make everything great possible" - Benjamin Franklin
My blog - Sharing My Thoughts, An Article - Understanding Statepattern
-
following code snippet works in VC++ 6 file = fopen((LPCTSTR)CSTRING_VARIABLE,"wb+") but in VC 2005 above code can not compile because of CString and casting problem. how I can convert above code to be compatible with visual C++ 2005. Regards Gut Mikh
Drop the casts. Use code like this:
#include <tchar.h>
// .... Stuff here ...
file = _tfopen(CSTRING_VARIABLE, _T("wb+"));Steve
-
I do agree with Mark's comment. When you create a new project in Visual C++ 2005, probably you might have not noticed the "Use Unicode Libraries" in the Wizard. While you are dealing with these it's better to use tchar functions. You can get those functions just by including #include Please post the error code if possible.
-Sarath_._ "Great hopes make everything great possible" - Benjamin Franklin
My blog - Sharing My Thoughts, An Article - Understanding Statepattern
i do agree with Sarath. when you change the charset in the project property sheet from "Use Unicode Libraries" to "Use MBCS Libraries",the compile problem with CString will disappear at once.