Thread: mbstowcs issue

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    9

    mbstowcs issue

    Hello, my procedure should take a string and parse all all values separated by comma ',' and store them in objQueue[qid].filtValue array. This array is of wide-char type.

    Code:
    typedef
    struct
    {
      char    *name;		
      int     filtValueCount;	
      wchar_t **filtValue;		
    } tQueueDef;
    tQueueDef *objQueue;
    
    /*
    qid - identifier (not important now)
    *s - pointer to parsed string
    *len - lenght of the parsed string
    *cnt - number of commas in the parsed string
    */
    void parseFiltValues(int qid, char *s, int *len, int *cnt)
    {
      int i, j, k;
      char *ptr, *tmp;
    
      tmp = (char *)malloc((MSG_LENGTH) * sizeof(char));
    
      printf("ParseFiltValues qid: %d, len: %d, cnt: %d\n", qid, *len, *cnt);
      if (*len == 0)
        return;
    
      objQueue[qid].filtValueCount = *cnt;
      objQueue[qid].filtValue = (wchar_t **)malloc(*cnt * sizeof(wchar_t *));
    
      for (ptr = s, i = j = k = 0; i <= *len; i++, j++)
      {
        switch (s[i])
        {
          case ',':
          case '}':
            strncpy(tmp, ptr, j);
            tmp[j] = '\0';
    
            objQueue[qid].filtValue[k] = (wchar_t *)malloc(MSG_LENGTH * sizeof(wchar_t));
            mbstowcs(objQueue[qid].filtValue[k], tmp, MSG_LENGTH);
    
            printf("filt val (%d): %s<%d> <%d>\n", k, tmp, strlen(tmp), wcslen(objQueue[qid].filtValue[k]));
    
            j = -1;
            ptr = ss + i + 1;  
            k++;
            break;
        } 
      }
    
      free(tmp);
    }
    So, for example, for input string {AA,AB,AD,AF}, the output should read:
    filt val(0): AA<2> <2>
    filt val(1): AB<2> <2>
    filt val(2): AD<2> <2>
    filt val(3): AF<2> <2>

    It works well until an Eastern European character arrives to the input. For input string {AB,AC,AČ,AŇA,AF,AFŠ,AZ} the output reads, which means the mbstowcs does not convert it properly.
    filt val(0): AA<2> <2>
    filt val(1): AC<2> <2>
    filt val(2): AČ<3> <1>
    filt val(3): AŇA<4> <1>
    filt val(4): AF<2> <2>
    filt val(5): AFŠ<4> <2>
    filt val(6): AZ<2> <2>

    Can anyone help me here please? It's weird because I use similar funtionality elsewhere in the program and there it works just fine... Thanks a lot!

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    The problem is that you're using the C locale, so mbstowcs is failing.
    Unicode/UTF-8 -- Going Beyond ASCII

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. K&R Learning Issue
    By TimHarrington in forum C Programming
    Replies: 48
    Last Post: 09-06-2010, 04:33 AM
  2. bandwidth issue / network issue with wireless device communication
    By vlrk in forum Networking/Device Communication
    Replies: 0
    Last Post: 07-05-2010, 11:52 PM
  3. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 04:56 AM
  4. type safe issue
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 02-12-2008, 09:32 PM
  5. my first issue of GDM
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 09-12-2002, 04:02 PM