首先,对不起,我知道这个问题被问了很多我什至读过几个答案,但仍然没有设法编写工作代码。这是我的基本循环,但它只读取最后一个节点,所以我做错了什么?
谢谢。
这是全球性的:
struct Inventory
{
int cod;
int quant;
float price;
char name[30];
struct Inventory *next;
};
struct Inventory *inventory = NULL;
这是读取的功能。
void load(void)
{
FILE *ptr;
ptr=fopen("inventory.dat", "rb+");
if (!ptr)
{
return;
}
struct Inventory *p;
while(!feof(ptr))
{
p = malloc(sizeof(struct Inventory));
fread(p, sizeof(struct Inventory), 1, ptr);
p->next = inventory;
inventory = p;
}
fclose(ptr);
}