I am really close here.
This reads the file in and using gdb I can see the words from my text file going to the linked list.
However when I print my linked list (bottom part of code) the whole linked list seems to just contain the last word from the file duplicated for however many entries are in the file.
bool load(const char* dictionary) {
// open dictionary
FILE* file = fopen(dictionary, "r");
if (file == NULL)
return false;
char buf[LENGTH];
//read the contents of the file and write to linked list
while (!feof(file)) {
fscanf(file,"%s", buf);
// try to instantiate node
node* newptr = malloc(sizeof(node));
if (newptr == NULL) {
return false;
}
// initialize node
newptr->next = NULL;
// add new word to linked list
newptr->dictword = buf;
//move node to start of linked list
newptr->next = first;
first = newptr;
}
fclose(file);
// traverse and print list.
node* ptr = first;
while (ptr != NULL) {
printf("%s\n", ptr->dictword);
ptr = ptr->next;
}
return true;
}