Thread: Linked List

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    5

    Unhappy Linked List

    I am trying to read data from a data file. But I don't know why the last record is failed to display.

    My datafile structure is like this:
    1
    Name1
    Credit1
    2
    Name2
    Credit2
    3
    Name3
    Credit3

    The last record (3 Name3 Credit3) is always missing in the display. Would you please give me some idea ??

    My source code is:

    struct student *ReadData(FILE* inFile)
    {
    struct student *head;
    struct student *ptr1,*ptr2;
    n=0;
    ptr1=ptr2=(struct student *)malloc(LEN);

    fscanf(inFile,"%d\n%s\n%d",&ptr1->studentID,ptr1->studentname, &ptr1->credit);
    head=NULL;

    while(ptr1->studentID!=0)
    {
    n=n+1;
    if(n==1){
    head=ptr1;
    }
    else{
    ptr2->next=ptr1;
    }
    ptr2=ptr1;
    ptr1=(struct student *)malloc(LEN);
    fscanf(inFile,"%d\n%s\n%d",&ptr1->studentID,ptr1->studentname, &ptr1->credit);

    }


    ptr2->next=NULL;

    fclose(inFile);
    return(head);
    }


    void DisplayRec(head)
    struct student *head;
    {
    struct student *ptr;
    printf("StudentID StudentName Credits\n");


    ptr=head;
    if(head!=NULL)

    do{
    printf("%5d%17s%11d\n",ptr->studentID,ptr->studentname,ptr->credit);
    ptr=ptr->next;
    }
    while(ptr!=NULL);
    }

    Thank you for giving help !!!!

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    5
    Sorry, the condition of the while loop should be

    while(!feof(inFile)){

    .....



    }


    not while(ptr->studentID!=0)


    Thanks.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > while(!feof(inFile)){
    This has to be one of the most common mistakes

    feof() does not become true UNTIL a read function (like fscanf) has failed to read something.

    This means that the last fscanf in your loop really did not read any data, and just returned EOF (which I guess you didn't test either).

    Try this
    Code:
    struct student *ReadData(FILE* inFile) 
    { 
        struct student *head = NULL; // the list
        struct student *ptr1; // the next node
        struct student temp; // file data
    
        while ( fscanf(inFile,"%d\n%s\n%d",
                &temp.studentID, temp.studentname, &temp.credit ) != EOF ) {
            // new node
            ptr1 = malloc( sizeof( struct student ) );
            ptr1->studentID = temp.studentID;
            // etc
            // now link ptr1 into the list starting at head
    }

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    5

    Wink Thanks

    Many Thanks for Giving Help!

    I will keep trying!!

  5. #5
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    What compiler are you using anyhow?

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. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM