linked list and file reading.

This is a discussion on linked list and file reading. within the C Programming forums, part of the General Programming Boards category; Hi, I have an assignment that I'm going to end up needed to create multiple linked lists after reading a ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    117

    linked list and file reading.

    Hi, I have an assignment that I'm going to end up needed to create multiple linked lists after reading a file.

    For now I'm just trying to understand how that part works. I'm just taking each line of the file and storing trying to store in the next node of the linked list for concepts sake. (later I'll need to split each line up and do a lot more to it, but this is just to understand the concepts of linked lists.)

    I'm getting an error of "assignment from incompatible pointer type". (lines 24, 29)
    My question is what am I doing wrong on this?


    Code:
    struct node{       
           char string[50];
           struct node *next;
    };
    
    
    int main()
    {
        FILE *fp;
        char buffer[50];
        struct node *head = NULL, *temp;    
        
            /* opens file */
        if ( (fp = fopen("dates.csv", "r" )) == NULL )
        {
        printf("Couldn't open file\n");
        exit(1); 
        }
       
         while(fgets(buffer, sizeof(buffer), fp))
        {                                                      
         temp = malloc( sizeof( struct node ) );
         strcpy(temp->string, buffer);
         temp->next = head;
         head = temp;
        }
        
        printf("\n");
        for(temp = head; temp != NULL; temp = temp->next)
            printf("%s\n", temp->string);

  2. #2
    Registered User
    Join Date
    Feb 2012
    Posts
    117
    Hmm nevermind. I changed
    Code:
    struct node *next
    from something I had it before and it worked. I'll have to work on these concepts some more to understand it more thoroughly. Sorry for the fail post

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading in file to linked list?
    By rickyson49 in forum C Programming
    Replies: 2
    Last Post: 03-31-2011, 09:07 AM
  2. Reading Text file in Linked list!
    By satty in forum C Programming
    Replies: 20
    Last Post: 07-29-2010, 08:48 AM
  3. Reading from a file into a linked list.
    By Wiretron in forum C Programming
    Replies: 5
    Last Post: 01-23-2006, 07:24 AM
  4. Reading a file into a Linked List
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 05-20-2002, 07:08 AM
  5. reading from a file to linked list
    By opacity in forum C++ Programming
    Replies: 3
    Last Post: 04-14-2002, 09:56 AM

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