Like Tree2Likes
  • 2 Post By Codeplug

Char Headaches

This is a discussion on Char Headaches within the C Programming forums, part of the General Programming Boards category; Hi folks, I am trying to set up a list of menu choices for a small device. What I want ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    5

    Thumbs down Char Headaches

    Hi folks,
    I am trying to set up a list of menu choices for a small device. What I want to do is list the choices in a const array, and show them up on screen one by one as the user cycles through the menu. Unfortunately, the device's code page is UTF16, so I need to preface each choice with an L and store it in a wchar_t variable.
    Here's what I have so far.
    In the global variables section, I set up the constant wchar_t array of choices.
    Code:
    const wchar_t* menuChoices[] = {
    L"Increase Brightness",
    L"Decrease Brightness",
    L"Check Temp",
    L"Save Temp",
    L"Exit"
    }; //menuChoices
    These will be individually copied into the TextToShow variable, which is picked up by the display function in another module.
    Code:
    extern char TextToShow[];
    So far so good. Now we move to the main function, when the user presses the Next button. I try to copy the next choice into the VarToShow variable, and that's when things go horribly wrong.
    Code:
    //Compute string length of this choice
    ccLen = wcslen (menuChoices[uCurrentChoiceIndex]);
    //Insure it will fit (128-byte limit)
    if (ccLen>128) ccLen=128;
    //Clear buffer (fill with null characters)
         memset(TextToShow, 0, 128);
    //Copy text into TTS Buffer
    memcpy (TextToShow, menuChoices[uCurrentChoiceIndex], ccLen);
    //Show the current choice
    ShowMenuText;
    (The device white-screens at this point, indicating a memory read or write error).
    However, the below code works fine.
    Code:
    memcpy (TextToShow, L"Increase Brightness", 59);
    ShowMenuText;
    I also tried using strcpy and it didn't crash, but it also only copied the first character (showed I instead of Increase Brightness).
    I realize that TextToShow is a char, but the compiler won't let me store L"Hello" in a char. But perhaps wchar_t isn't the correct variable type for menuChoices, either.
    Have you any idea what I'm doing wrong? If you can write out an example of the correct procedure, I'd be so thankful!
    Thank you so much for reading! I'm completely baffled. Any help at all would be greatly appreciated!
    Last edited by SunnyDays; 11-12-2012 at 09:38 AM.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    If you play around with wide characters, it would be good to use the appropriate functions, as wcscpy

    Also i remember that once you play with wide characters, you have to set the locale.

    For a example we had this in class exercise and we wrote this code
    Code:
    #include <assert.h>
    #include <string.h>
    #include <stdio.h>
    #include <wchar.h>
    #include <locale.h>
    
    typedef struct {
        wchar_t term[10];
        int freq;
    } word_t;
    
    /* global array of words */
    word_t words[5000];
    int n_words = 0;
    
    int find_word(wchar_t s[]) {
        int i;
        for (i=0; i < n_words; i++)
            if ((wcslen(s) == wcslen(words[i].term)) && wcsncmp(s, words[i].term,10) == 0)
                return i;
        return -1;
    }
    
    void add_word(wchar_t s[]) {
        assert(wcslen(s) <= 10);
        wcsncpy(words[n_words].term, s,10);
        words[n_words].freq = 1;
        n_words++;
    }
    
    int main(void) {
        wchar_t s[11];
        wint_t c;
        int i;
        int counter = 0 ;/*use in inserting characters in s*/
        setlocale(LC_CTYPE,"UTF-8");
        printf("Please type a word with 10 characters at most\n");
        while (( c = getwchar() ) != WEOF) {
            if( counter > 10 )
            {
                printf("Word with more than 10 characters detected,so exiting...\n");
                return -1;
            }
            if( c != '\n' )
            {
                s[counter++] = c;
            }
            else
            {
                i = find_word(s);
                if (i == -1)
                {
                    add_word(s);
                    /*wprintf(L"1: %0.10ls\n", s);*/
                }
                else
                {
                    words[i].freq++;
                    /*wprintf(L"%d: %0.10ls\n", words[i].freq, s);*/
                }
                for( i = 0 ; i < 11 ; i++)/*clear the buffer*/
                {
                   s[i] = '\0';
                }
                counter = 0;/*reset counter*/
    
                printf("Please type a word with 10 characters at most\n");
            }
        }
        printf("RESULTS : \n");
        for( i = 0 ; i < n_words ; i++)
        {
            wprintf(L"%d: %0.10ls\n", words[i].freq, words[i].term);
        }
        return 0;
    }

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,630
    >> UTF16, so I need to preface each choice with an L and store it in a wchar_t variable
    The size and encoding of a wchar_t is implementation defined. On Windows OS's, it's UTF16-LE. On many other OS's it's UTF32 with host-endianess.

    >> extern char TextToShow[]
    How big is this buffer? 128 bytes?

    >> ccLen = wcslen (menuChoices[uCurrentChoiceIndex]);
    This give you the number of wchar_t's, not the number of bytes.

    gg

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    5
    Thank you both so much for your replies! I will try wcscpy. In the meantime, can you tell me how to get the number of bytes in a wchar? Obviously, wcslen is not the answer. Thanks guys, you are awesome!

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by SunnyDays View Post
    In the meantime, can you tell me how to get the number of bytes in a wchar?
    sizeof(wchar_t) gives you the number of bytes of one wide character, thus sizeof(wchar_t) * (wcslen(string) + 1) gives you the size of "string" (including the terminating '\0') in bytes.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fstream open method causing me a few headaches
    By Finchie_88 in forum C++ Programming
    Replies: 8
    Last Post: 10-18-2010, 06:44 PM
  2. Caesar cipher bug headaches.
    By Arik in forum C Programming
    Replies: 9
    Last Post: 05-15-2010, 10:06 PM
  3. mingw environment gives me headaches...
    By pheres in forum C++ Programming
    Replies: 7
    Last Post: 08-16-2007, 01:34 PM
  4. Linked List headaches
    By Strait in forum C++ Programming
    Replies: 3
    Last Post: 02-16-2005, 07:40 PM
  5. DirectMusic headaches...
    By jdinger in forum Game Programming
    Replies: 1
    Last Post: 04-13-2002, 04:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21