私はここに本当に近いです。
これによりファイルが読み込まれ、gdbを使用して、テキストファイルからリンクリストに移動する単語を確認できます。
ただし、リンクリスト(コードの下部)を印刷すると、リンクリスト全体に、ファイルに複製されたファイルの最後の単語が含まれているように見えますが、ファイルには多くのエントリがあります。
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;
}