Liquidmetal
Liquidmetal

Reputation: 295

Read integers into linked list

this is for a project for class. (C language)

I am having trouble with the first function that I'll need to implement. It's a load function where I take an input filename, open up a file given by user at runtime, and read it into a linked list format from a file with an integer on each line.

I am stuck with how to create the linked list so that they are all actually linked, without the same node in the list being overwritten each time.

This is what I have so far, which I know is incorrect.

#include"sortingheaders.h"
#include <stdio.h>
#include<stdlib.h>
#include<time.h>

///////////////////////////////
Node* List_Create(Node * ln)
{
  if(ln==NULL)
    { 
   ln = malloc(sizeof(Node));
      ln->value = 0;
      ln->next = NULL;
    }
  return ln;
}

/////////////////////////////////////////////////////////////////////////////
Node* Load_File(char *Filename)
{
  //Open file
  FILE* fptr = fopen(Filename, "r");
  Node* ln=NULL;
  Node* temp=NULL;
  long int *x = 0;
//Validity Check, return 0 if unsuccesful
  if(fptr ==NULL)
    {printf("File didnt open!"); return 0;}
 ln= List_Create(ln);
  while(!feof(fptr))
    {
      fscanf(fptr,"%li",x);
      ln->value = *x;
      ln->next = List_Create(temp);
      return(ln);
    }

Upvotes: 0

Views: 1444

Answers (1)

Mitchell
Mitchell

Reputation: 56

At the first of the while loop add this.

    temp = NULL;

Upvotes: 2

Related Questions