NO

Author Topic: Little squares at end of combobox text lines  (Read 3759 times)

grom-it

  • Guest
Little squares at end of combobox text lines
« on: August 12, 2007, 09:54:45 PM »
I must be missing something simple here

so any help would be appreciated

the file referred to in the following code has two items in it
"ADD NEW" and "ADD ANOTHER" ( for test purposes only you understand)

The output to a combobox always shows squares at the end of the text  my assumption is that these are the printed version of "\n" "\0" or NULL

The question is how to prevent their output?
info All wchar_t being used
else
    {
      // file is open for read so read file into combobox
      do
         {
            c = fgetws(a_line,30,ptrcomboxfile);  /* get one line from the file */
            if (c != NULL)
             
      comboindex = SendDlgItemMessage(g_hwnd, IDC_COMBOBOX, CB_ADDSTRING, 0, (LPARAM)a_line);
       
       } while (c != NULL);              /* repeat until NULL          */

     }
      fclose(ptrcomboxfile);

severach

  • Guest
Re: Little squares at end of combobox text lines
« Reply #1 on: August 22, 2007, 08:56:05 AM »
They are not the printed version of \0 because Windows specifically stops at the \0 character. They are the printed versions of \r\n which are at the end of all MS-DOS terminated lines and fgets() does not trim these for you like gets() does. Write a for loop to trim off all trailing whitespace. Here's some untested code that might work:

Code: [Select]
for(wchar_t *c=wcschr(a_line,L'\0'); c>a_line && iswspace(c[-1]); --c);
*c=L'\0';

This code is made to be easy to read, not efficient. I wouldn't use wcschr, potentially too slow. I wouldn't ask the compiler to calculate the [-1] offset on each read, I'd do the extra pointer work myself.

grom-it

  • Guest
Re: Little squares at end of combobox text lines
« Reply #2 on: August 23, 2007, 12:58:56 AM »
Thanks For the point in the right direction Severach


Once i realised what i was dealing with
further googles revealed this little bit of simplicity

lenofstr=wcslen(a_line);
a_line[lenofstr-1]='\0';

It works
But as it is so simple do you or anyone else see any problems in using it
seems to do the job.

i want to display fairly short strings to a combobox ?

severach

  • Guest
Re: Little squares at end of combobox text lines
« Reply #3 on: August 25, 2007, 01:00:44 AM »
Your code works fine so long as there is exactly one character at the end that needs to be destroyed. I figure there might be 0, 1, or 2 characters. 0 characters means that one real character at the end will be destroyed. 2 means that 1 box will still show. I wrote it as a loop to ensure that any number of whitespace characters at the end including 0 would be removed.

Code: [Select]
Som
Line
Coul
Com
Ou
Lik
Thi

Others[]
Could[][][]
Look[][][]
Your program has performed an illegal operation.

Your code also suffers from a buffer overflow. If you are sent a blank string "", you will erase the byte before the beginning of the string. 1337 haxors rejoice with yet another way to pown the world.

If you're confident that all lines will have exactly one undesirable character at the end then your code will do fine. While I like fast and simple code, the potential problems justify no less than the adaptive loop for me.

Over time you'll learn to recognize the skill of the programmer behind the code and know how to adjust accordingly.