I want to read in numbers from text file and create a linked list dynamically, although I am able to do it, the last node with value of 0 is also added to the list. How can I correct this? Also I should be able to add value 0 if i want but not in this way.
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node *next;
}node;
int main(int argc, char const *argv[]) {
FILE *fp = fopen("data.txt", "r");
node *prev = NULL;
while(!feof(fp)){
node *curr = malloc(sizeof(node));
fscanf(fp, "%d", &(curr->data));
fprintf(stdout, "%d-->", curr->data);
if(prev != NULL){
prev->next = curr;
}
prev = curr;
}
printf("NULL\n");
fclose(fp);
return 0;
}
The input file is this single column of integers 1 5 2 3 6 4
The output comes like this, the 0 is not part of input 1-->5-->2-->3-->6-->4-->0-->NULL