0

I'm having trouble reading in values to a linked list using fscanf in a loop.

The program ultimately compiles and runs, but the print function is not producing any output which leads me to believe that my linked list is not actually being created in my read function.

I set up a few error checking printf statements and discovered that my student *head value is NULL when it is returned back to main and then sent to the print function.

My functions:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct Student{
    char* first;
    char* last;
    int i;
    int j;
    struct Student* next;
}student;

int main(int argc, char* argv[])
{
    student *start = NULL;
    char *studentInput = argv[1];
    start = buildStudentList(studentInput,start);
    printStudents(start);
    free_students(start);
    return 0;
}

student* buildList(char* studentsFile, student* head) 
{
    student *current = head;
    char *f = malloc(sizeof(char) * 25);
    char *l = malloc(sizeof(char) * 25);
    int prior,level = 0;
    FILE *in = fopen(studentsFile, "r");

    if(in == NULL)
    {
        printf("\nThe input file failed to open\n");
        return NULL;
    }
    else
    {
        while(!feof(in))
        {
            fscanf(in, "%s %s %d %d",f,l,&prior,&level);
            student *new = (student*)malloc(sizeof(student));
            new->first = (char*)malloc(sizeof(char) * 25);
            new->last = (char*)malloc(sizeof(char) * 25);
            current = new;
            strcpy(current->first,f);
            strcpy(current->last,l);
            current->i = prior;
            current->j = level;
            current->next = NULL;
            current = current->next;
        }
        free(first);
        free(last);
        fclose(in);
        return head;
    }
}

void print(student* head) 
{
    if(head == NULL)
    {
        printf("head is null");
        return;
    }

    student *current = head;

    while(current->next != NULL)
    {
        printf("\n\nFirst Name: %s\nLast Name: %s\nPriority: %d\nReading Level: %d\n\n", current->first, current->last, current->i, current->j);
        current = current->next;
        printf("makes to loop");
    }
}

void free_students(student *head)
{
    if(head == NULL)
    {
        return;
    }

    student *current = head;
    while(current->next != NULL)
    {
        student *temp = current;
        current = current->next;
        free(temp->first);
        free(temp->last);
        free(temp);
    }
    free(current->first);
    free(current->last);
    free(current);
}

At this point the output is "head is null".

  • 2
    Note that [`while (!feof(file))` is always wrong](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong) applies to this code. – Jonathan Leffler Mar 16 '17 at 04:55
  • 1
    "The program ultimately compiles ": have you turned on all warning flags? Are there any warnings? Have you littered your program with printf functions for (simplistic) debugging purposes to try and track variables and their values? Have you tried trimming down the program to the actual problem *only*? –  Mar 16 '17 at 04:55
  • 1
    Welcome to Stack Overflow. Please read the [About] and [Ask] pages soon, but even more importantly, please read about how to create an MCVE ([MCVE]). One important part of an MCVE is the (minimal) input data, and the actual output and the expected output. – Jonathan Leffler Mar 16 '17 at 04:56
  • 1
    These two lines `current->next = NULL; current = current->next;` fill me with concern. Are you sure that's what you wanted to happen? The last line looks completely wrong, setting `current` to `NULL`. – Jonathan Leffler Mar 16 '17 at 04:58
  • 1
    By the way, [`sizeof(char)` is always 1 (in C99)](http://stackoverflow.com/questions/2215445/are-there-machines-where-sizeofchar-1-or-at-least-char-bit-8), and [don't cast the return value of `malloc` in C](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). –  Mar 16 '17 at 04:59
  • 1
    `student *current = head;` ... `current = new;` .. `return head;` : `head` has not been changed in `buildList`. – BLUEPIXY Mar 16 '17 at 05:23

0 Answers0