using listboxes and fgetc()

This is a discussion on using listboxes and fgetc() within the Windows Programming forums, part of the Platform Specific Boards category; hey I am using listboxes and fgetc to add a comma delimited string to my listbox but I am always ...

  1. #1
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    230

    using listboxes and fgetc()

    hey I am using listboxes and fgetc to add a comma delimited string to my listbox but I am always getting segmentation faults, here is the code (please help I am almost done with this app).

    Code:
    hwndlistbox = CreateWindow(TEXT("Listbox"), NULL, WS_CHILD | WS_VISIBLE | WS_BORDER | ES_MULTILINE,
    				50, 70, 170, 140, hwnd, (HMENU) ID_EDIT,
    				NULL, NULL);
                    char buff1;
                    
                     pfile = fopen("C:\\reminder\\data\\backup1.xls", "r");
                     atoi(&buff1)
                     while( (buff1 = fgetc(pfile)) != EOF)
    {
       /* filter out unwanted characters*/
    
           SendMessage(hwndlistbox, LB_ADDSTRING, 0, (LPARAM)buff1);
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,540
    atoi() expects a pointer to a char ARRAY, which ends with a \0 character.

    A single character (unless it is set to \0) is not valid (even though the code is syntactically correct).

    You need to focus more on what the function actually does, rather than just declaring variables and passing parameters with the correct type.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    ES_MULTILINE is not a listbox style.

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    Quote Originally Posted by Salem View Post
    atoi() expects a pointer to a char ARRAY, which ends with a \0 character.

    A single character (unless it is set to \0) is not valid (even though the code is syntactically correct).

    You need to focus more on what the function actually does, rather than just declaring variables and passing parameters with the correct type.
    I agree, but if being picky...

    atoi() will stop as soon as it hits a character that is not a digit or part of a number (which depends on the local settings ie is a decimal point a period or a comma), it does not 'require' a null terminated string.

    I use this 'feature' quite a bit (ie iDate = atoi("20100714_SomeFileName.ext") is valid in MSVC)

    I would not use a single character as I would be worried about what random value was in the next memory address (if it happens to be a digit then....)
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

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