图书信息第一个可以完整的输出但是从第二个起他的图书信息中的编码就消失了#include stdioh#include stdlibh#include stringhtypedef struct Node char bookCode50; char bookName50; char author50; char publisher50; char bookkind50
在loadBookData()函数中,每次读取图书信息时,都需要在fscanf语句后面添加一个空格,以消耗换行符。修改后的loadBookData()函数如下:
void loadBookData() {
FILE *file = fopen("book_storage.txt", "r");
if (file == NULL) {
return;
}
while (!feof(file)) {
BookNode *newNode = (BookNode *)malloc(sizeof(BookNode));
fscanf(file, "%s ", newNode->bookCode);
fscanf(file, "%s ", newNode->bookName);
fscanf(file, "%s ", newNode->author);
fscanf(file, "%s ", newNode->publisher);
fscanf(file, "%s ", newNode->bookkind);
fscanf(file, "%s ", newNode->bookkinds);
fscanf(file, "%d ", &newNode->quantity);
fscanf(file, "%d ", &newNode->borrowed);
newNode->next = NULL;
if (head == NULL) {
head = newNode;
} else {
BookNode *current = head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
}
fclose(file);
}
这样就能够正确读取图书信息文件中的编码了。在其他函数中不需要进行修改
原文地址: https://www.cveoy.top/t/topic/hKtK 著作权归作者所有。请勿转载和采集!