fgets() to list box

This is a discussion on fgets() to list box within the Windows Programming forums, part of the Platform Specific Boards category; I have a couple questions I couldn't find solutions for. Not sure if I'm just not searching for the right ...

  1. #1
    tao
    tao is offline
    Registered User
    Join Date
    Jun 2006
    Posts
    26

    fgets() to list box

    I have a couple questions I couldn't find solutions for. Not sure if I'm just not searching for the right terms...

    I'm reading in lines from a text file using fgets() and sending the output to a list box. Unfortunately, since fgets() also reads in the null terminator, this is displayed in the list box as a pipe (|) at the end of the string in the list box display. (At least I'm assuming this is the cause.)

    I am wondering how this can be avoided. I've tried a few different methods, including writing the char array to a new one and terminating when it gets to the null char, but for some reason this wouldn't work either.

    As an aside, my main problem with this null terminator arises when I remove items from the list box and I have the program read each value remaining in the list box after the removal and truncate the file to re-write only the remaining values. But if you removed a value with a null terminator showing in the list box, there is a blank line inserted in the re-written file at this position.


    My code to read from file to list box is:

    Code:
    FILE *inp;
    inp = fopen("file.txt", "r");
    
    if (inp != NULL)
    {
    	char FileInput[1001] = {'\0'};
    
    	while(fgets(FileInput, 1000, inp)!=NULL)
    	{
    		SendDlgItemMessage(hwnd, IDC_LIST, LB_ADDSTRING, 0, (LPARAM)FileInput);
    	}
    
    	fclose(inp);
    }

    And the code to re-write the file after removal of selected items:

    Code:
    // Get number of items remaining in list box
    count = SendMessage(hList, LB_GETCOUNT, 0, 0);
    
    // Initialize index array
    index = GlobalAlloc(GPTR, sizeof(int) * count);
    
    // Write remaining items to truncated file
    FileData = (char *)malloc(sizeof(char) * 1001);
    
    // Find the index value, buffer data at the index, and write it to file
    for(i = 0; i < count; i++)
    {
    	index = (long *)SendMessage(hList, LB_GETITEMDATA, (WPARAM)i, 0);
    	SendMessage(hList, LB_GETTEXT, (WPARAM)(index+i), (LPARAM)FileData);
    	fprintf(mod, "%s\n", FileData);
    }

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    288
    i think fgets breaks on '\n' characters, and the file is probably using crlf encoding '\r\n'.
    you can fix that using:

    Code:
    while(fgets(FileInput, 1000, inp)!=NULL)
    	{
    		FileInput[strlen(FileInput) - 1] = '\0';
    		SendDlgItemMessage(hwnd, IDC_LIST, LB_ADDSTRING, 0, (LPARAM)FileInput);
    	}

  3. #3
    tao
    tao is offline
    Registered User
    Join Date
    Jun 2006
    Posts
    26
    Brilliant. That's just what I was looking for, thanks.

  4. #4
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>I am wondering how this can be avoided.<<

    The faq has an example showing how to remove the end of line character from the string read by fgets:

    FAQ > How do I... (Level 1) > Get a line of text from the user/keyboard (C)

    Welcome to the boards.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  5. #5
    tao
    tao is offline
    Registered User
    Join Date
    Jun 2006
    Posts
    26
    Ah, I completely overlooked that. Thanks. And for the welcome. =]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursion Revisited again, and again!
    By clegs in forum C++ Programming
    Replies: 93
    Last Post: 12-08-2007, 08:02 PM
  2. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 11:01 AM
  3. List box horizontal bar
    By cfriend in forum Windows Programming
    Replies: 1
    Last Post: 09-12-2004, 04:52 PM
  4. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 09:27 PM

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