Using strtok() in a UNICODE MFC Application
-
I just wanted to parse some data read into an CStringArray. The string to be parsed is got out of the array and is a CString type. I have been trying to use strtok() to do this. Typical string is "qwerty,123,,33,ABCD,,4545," I have another member variable (CString type array) to hold the parsed strings (as CString). Typical string is "qwerty,123,,33,ABCD,,4545," sent to the GetDataItem procedure.
void CGenerate_FilesDlg::GetDataItem(CString str)
{
// token is ","
char *P1 = strtok((char*)(LPCTSTR)str, ",");
char *P2 = strtok(NULL, "\0");//m\_ParseParams\[0\].Format("%s",P1); //m\_ParseParams\[1\].Format("%s",P2);
}
I get error when trying to put the parsed data into m_ParseParams[]
Error 3 error C2664: 'void ATL::CStringT<BaseType,StringTraits>::Format(const wchar_t *,...)' : cannot convert parameter 1 from 'const char [3]' to 'const wchar_t *' c:\generate_filesdlg.cpp 225
I am having problems with the UNICODE. Because I am using CStringArray I seem to be having problems using functions like strtok() in the VS 2005 environment. What I am trying to do is so simple, but in a mess with types! Andy.
-
I just wanted to parse some data read into an CStringArray. The string to be parsed is got out of the array and is a CString type. I have been trying to use strtok() to do this. Typical string is "qwerty,123,,33,ABCD,,4545," I have another member variable (CString type array) to hold the parsed strings (as CString). Typical string is "qwerty,123,,33,ABCD,,4545," sent to the GetDataItem procedure.
void CGenerate_FilesDlg::GetDataItem(CString str)
{
// token is ","
char *P1 = strtok((char*)(LPCTSTR)str, ",");
char *P2 = strtok(NULL, "\0");//m\_ParseParams\[0\].Format("%s",P1); //m\_ParseParams\[1\].Format("%s",P2);
}
I get error when trying to put the parsed data into m_ParseParams[]
Error 3 error C2664: 'void ATL::CStringT<BaseType,StringTraits>::Format(const wchar_t *,...)' : cannot convert parameter 1 from 'const char [3]' to 'const wchar_t *' c:\generate_filesdlg.cpp 225
I am having problems with the UNICODE. Because I am using CStringArray I seem to be having problems using functions like strtok() in the VS 2005 environment. What I am trying to do is so simple, but in a mess with types! Andy.
Most CRT string functions have a TCHAR.H generic string type equivalent. Since CString is a generic string type you should use generic string CRT functions. Try _tcstok(). You may also want to look into using equivalent CStringT methods instead of using CRT functions on CStrings. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Most CRT string functions have a TCHAR.H generic string type equivalent. Since CString is a generic string type you should use generic string CRT functions. Try _tcstok(). You may also want to look into using equivalent CStringT methods instead of using CRT functions on CStrings. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
Hi Mark, can you give me a few pointers as to use MFC C++ with UNICODE. I was just using the following:- CString str = _T("qwerty"); and then casting my way out of any problems. I seem to have come unstuck. Just need help with project options, header files and settings, if possible. Many thanks, Andy.
-
Hi Mark, can you give me a few pointers as to use MFC C++ with UNICODE. I was just using the following:- CString str = _T("qwerty"); and then casting my way out of any problems. I seem to have come unstuck. Just need help with project options, header files and settings, if possible. Many thanks, Andy.
Andy202 wrote:
Just need help with project options, header files and settings, if possible.
What do you mean? :confused: Either
UNICODE
is defined (via#define
) or it isn't."Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
-
Hi Mark, can you give me a few pointers as to use MFC C++ with UNICODE. I was just using the following:- CString str = _T("qwerty"); and then casting my way out of any problems. I seem to have come unstuck. Just need help with project options, header files and settings, if possible. Many thanks, Andy.
Andy202 wrote:
project options, header files and settings
MFC already uses the Generic-Text Mappings[^] so there's no additional header files to worry about. You can use the Configuration Properties/General/Character Set project setting to choose Unicode or MBCS.
Andy202 wrote:
a few pointers as to use MFC C++ with UNICODE
Since MFC uses CString, I prefer to use CStrings for all my strings. If a CRT string function is needed (because there's no equivalent functionality in the CString class) then it's easiest to use generic-text Routine Mappings[^] instead of using the char specific CRT string functions. This keeps all the code buildable regardless of whether it's a Unicode or MBCS build.
Andy202 wrote:
casting my way out of any problems
If you use the generic text stuff described above, you shouldn't (very rarely at least) need casts. If the compiler complains, it may be that you need to convert a string to the type expected. Just remember Cstrings are wchar_t-based on Unicode builds, otherwise they are char-based. If you need to force a CString to a specific type regardless of the build configuration, then use the specific CStringA or CStringW type. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java: