【发布时间】:2014-01-22 20:19:06
【问题描述】:
好的,这是我的问题,我有 2 个结构,客户端和项目。基本上项目是客户端内的嵌套列表。我不知道如何从文件中读取所有项目并在阅读时将它们链接到客户端。我知道如果我只有客户端结构并且必须仅从文件中读取客户端,那么它会如何完成,但至少对于我来说,当我有内部列表时它非常复杂。如何在文件中将项目与客户分开,以便在阅读它们并重新链接程序时知道输入是什么?我不想计算有多少项目链接到每个客户以及在保存之前有多少客户。这是我的结构和保存功能。
struct item
{
char item_name[30];
char item_state[30];
float item_price;
char item_status[30];
float item_price_if_not;
struct item *next;
};
struct client
{
char client_name[30];
char client_last_name[30];
struct item *item_data;
struct client *next;
};
void savetxt(struct client *head)
{
FILE *f;
f = fopen("data.txt","w");
if(f == NULL)
{
printf("error");
}
struct item *CurrentItem = head->item_data;
while(head != NULL)
{
fprintf(f,"%s %s\n",head->client_name,head->client_last_name);
while(CurrentItem != NULL)
{
fprintf(f,"%s %s %f %s %f ",CurrentItem->item_name,CurrentItem->item_state,CurrentItem->item_price,CurrentItem->item_status,CurrentItem->item_price_if_not);
CurrentItem = CurrentItem->next;
}
head = head->next;
if(head != NULL)
{
CurrentItem = head->item_data;
}
fprintf(f,"\n\n");
}
fclose(f);
return NULL;
}
【问题讨论】:
标签: c file linked-list