【问题标题】:Reading from file to linked list从文件读取到链表
【发布时间】: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


    【解决方案1】:

    有很多方法。考虑使用'('')'

    您需要一些分隔符或关键字来衬托数据。通过用分隔符封装每个结构,读取可以确定何时开始或完成新级别的结构。

    int foo(void) {
      ...
      fputc('(', f);
      while (head != NULL) {
        fputc('(', f);
        fprintf(f, "%s %s\n", head->client_name, head->client_last_name);
        while (CurrentItem != NULL) {
          fputc('(', f);
          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;
          fputc(')', f);
        }
        head = head->next;
        if (head != NULL) {
          CurrentItem = head->item_data;
        }
        fputc(')', f);
        fprintf(f, "\n\n");
      }
      fputc(')', f);
    }
    

    【讨论】:

    • 但是我如何识别文件中有限制标记,例如“)”?读取文件时有什么功能可以这样做吗?
    • 你能解释一下我到底该怎么做吗?
    • 文件由数据分隔符组成。如果您希望client_last_name 包含空格,那么您都准备好了限制,因为您可能将空格用作分隔符而不是数据的一部分。典型的解决方案是“不允许姓氏中的空格”,这并不可靠,但对于学生计划来说可能就足够了。同样,禁止字符串中出现 '(' 和 ')'。然后使用scanf()"%[^() ]" 读取一个既不包含空格也不包含'(' 和')' 的字符串。另一种解决方案是转义序列。
    【解决方案2】:

    检查什么是指针 swizzling 和 unswizzling 或序列化反序列化。来自维基百科:

    pointer swizzling 是基于名称或引用的转换 指向指针引用的位置。它通常被执行 在可重定位对象的反序列化(加载)期间 磁盘,例如可执行文件或基于指针的数据结构。这 反向操作,用与位置无关的指针替换 符号或位置,有时被称为 unswizzling,并且是 在序列化期间执行(保存)

    基本上,您需要为每个结构添加一个唯一的 id,并在存储到磁盘和读取时使用该 id 访问您的结构实例。 检查herehere

    【讨论】:

    • 对我来说似乎过于复杂,因为我对编程很陌生:x
    • @user3209183 听起来很复杂,因为这些名字混杂,混杂。这个想法很简单,添加一个 id 并通过 id 引用您的结构。
    猜你喜欢
    • 2019-10-17
    • 2019-09-22
    • 2018-06-02
    • 1970-01-01
    • 1970-01-01
    • 2021-09-15
    • 2017-04-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多