Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Using strtok() in a UNICODE MFC Application

Using strtok() in a UNICODE MFC Application

Scheduled Pinned Locked Moved C / C++ / MFC
c++visual-studiodata-structureshelpworkspace
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Andy202
    wrote on last edited by
    #1

    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.

    M 1 Reply Last reply
    0
    • A Andy202

      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.

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      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:

      A 1 Reply Last reply
      0
      • M Mark Salsbery

        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:

        A Offline
        A Offline
        Andy202
        wrote on last edited by
        #3

        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.

        D M 2 Replies Last reply
        0
        • A Andy202

          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.

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • A Andy202

            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.

            M Offline
            M Offline
            Mark Salsbery
            wrote on last edited by
            #5

            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:

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups