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".

6
  • 2
    Note that while (!feof(file)) is always wrong applies to this code. Mar 16, 2017 at 4: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?
    – user707650
    Mar 16, 2017 at 4:55
  • 1
    Welcome to Stack Overflow. Please read the About and How to Ask pages soon, but even more importantly, please read about how to create an MCVE (minimal reproducible example). One important part of an MCVE is the (minimal) input data, and the actual output and the expected output. Mar 16, 2017 at 4: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. Mar 16, 2017 at 4:58
  • 1
    By the way, sizeof(char) is always 1 (in C99), and don't cast the return value of malloc in C.
    – user707650
    Mar 16, 2017 at 4:59

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.