Thread: linked list returning unexpected value

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    6

    linked list returning unexpected value

    Hi,

    I'm having a bit of trouble with this linked list. It imports a ten-line text file, and the last lines of the code are supposed to print the 1st line of the text file, but instead it prints the last line of it. Been working on this a while and still can't figure it out.
    I've trimmed down the code a bit, but these basics still illustrate the problem I've been having.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    /*
    define BLOQ
    */
    
    typedef struct BLOQ {
         char *lineptr; /* text of line */
         struct BLOQ *link;
         struct BLOQ *prevlink;
         int nodeLength;
        /* pointer to next line */
    } BLOQ;
    
    
    int main (void)
    {
    char inbuff[ 82 ]; 
    char file_name[ 82 ];
    char *tmpchr;
    int numline;
    FILE *fp;
    BLOQ top;
    BLOQ *temp = NULL;
    BLOQ *bottom = NULL;
    
    
    /*
    assign mem block for tmpchr
    */
    
    tmpchr = malloc(82);
    
    /*
    assign mem block for bottom
    */
    bottom = malloc(40);
    
    /*
    assign value to bottom->lineptr
    */
    bottom->lineptr = "[BOF]\n";
    /*
    assign value to top
    */
    
    /*
    get file
    */
    printf( "\nEdit which file? " );
         fgets( inbuff, 82, stdin );
         sscanf( inbuff, "%s", file_name );
         printf( "\n" );
         
    fp = fopen( file_name, "r" );
    
    
    temp = malloc(40);
    bottom -> link = temp;
    top = *bottom;
    /*
    move to next node
    */
    bottom = bottom->link;
    
    /*
    fill nodes from file
    */
    for(numline=1 ; fgets( tmpchr, 82, fp ) != NULL ; numline++)
    {
    bottom -> lineptr = tmpchr;
    temp = malloc(40);
    bottom -> link = temp;
    bottom = bottom -> link;
    }
    
    
    temp = &top;
    printf("top lineptr is %s\n",top.lineptr);
    
    printf("temp is %s\n",temp->lineptr);
    
    /* move to next node */
    temp = top.link;
    
    /* should print first line of text file, but prints last instead,
    which is also the value of tmpchr, but not sure why*/
    
    printf("temp is %s\n",temp->lineptr);
    
    
    
    
    return 1;
    
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    In the loop you read everytime into the same char buffer.
    try
    Code:
        for(numline=1 ; fgets( tmpchr, 82, fp ) != NULL ; numline++) {
            bottom -> lineptr = tmpchr;
            temp = malloc(40);
            bottom -> link = temp;
            bottom = temp;
            tmpchr = malloc(82);  /* allocate space for another string */
        }
    also you should replace
    Code:
    bottom = malloc(40);
    with
    Code:
    bottom = malloc(sizeof(BLOQ));
    Kurt

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    6
    Cool, thanks Kurt!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. returning a linked list from a function
    By jlshipman in forum C++ Programming
    Replies: 3
    Last Post: 01-23-2006, 07:05 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM