I'm trying to read a text file in C and insert all of the words in the file to a linked list which will then later be traversed.
I've coded my program to take a file specified at the command line, open it and read each word one by one inserting it into the list. However on compiling and running the program it seems to go into an infinate loop, taking up a lot of processing power and writing lots to the disk (on both windows and linux systems).
If anyone can help, that'd be great.
Code:
#include <stdlib.h>
#include <stdio.h>
typedef struct L {char *word; struct L *next;} List;
List *insertlist(char *head, List *tail) {
List *t = calloc(1, sizeof(List));
t->word = head;
t->next = tail;
return t;
}
int *makelist(FILE *fp) {
List *wordlist;
char *word;
while (feof(fp) == 0) {
fscanf(fp, "&d", &word);
insertlist(word, wordlist);
}
return 0;
}
int main (int argc, char *argv[]) {
FILE *fp;
if (fopen(argv[1], "r") != 0) {
fp = fopen(argv[1], "r");
makelist(fp);
fclose(fp);
} else {
printf("No such file\n");
}
return 0;
}
thanks in advance.