微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用在 c 中具有嵌套结构的链表打印到文件

如何解决使用在 c 中具有嵌套结构的链表打印到文件

这个程序有两个错误

1.文件输出不像预期的那样,它正在转储整个东西,因为 while (strcmp((*head)->details.content[i],"END") != 0) 虽然我已经将内容数组的最后一个元素初始化为 END

  1. 如果我使用 for with some i<5 来克服这个问题;我没有得到正确的输出,即 第一列 ORDERID 刚刚更新,尽管提供了额外的数据,但其余数据并未更新 它永远不会超出第一行。(参考下面给出的输出

     #include <stdio.h>
     #include <stdlib.h>
     #include <string.h>
     int ORDERID = 1;
     int gender;
     struct item
     {
         struct data
         {
             char content[10][20];
         } details;
         struct item *next;
     };
     typedef struct item product;
     struct data *get_newdata()
     {
         struct data *tmp = (struct data *)malloc(sizeof(struct data));
         if (tmp == NULL)
         {
             printf("RAM full");
             return NULL;
         }
         return tmp;
     }
     struct data init_Saree()
     {
         struct data *new_data = get_newdata();
         ORDERID++;
         printf("We have Saree to offer to you !!\n");
         strcpy(new_data->content[0],"OrderName: Saree"); // orderName
         printf("Enter the gender (1 for Girl | 2 for Women): ");
         scanf("%d",&gender);
         if (gender == 1)
             strcpy(new_data->content[1],"Gender: Girl");
         else if (gender == 2)
             strcpy(new_data->content[1],"Gender: Woman");
         strcpy(new_data->content[2],"Length= 6.5mts");
         printf("Enter The Material(Cotton/Silk): ");
         scanf("%s",new_data->content[3]);   // material
         strcpy(new_data->content[4],"END"); // used as a deliminator
         return *new_data;
     }
     struct data init_Tshirt()
     {
         struct data *new_data = get_newdata();
         ORDERID++;
         printf("We have T-shirt to offer to you !!\n");
         strcpy(new_data->content[0],"OrderName: T-shirt"); // orderName
         printf("Enter the gender (1 for Boy | 2 for Men): ");
         scanf("%d","Gender: Boy");
         else if (gender == 2)
             strcpy(new_data->content[1],"Gender: Men");
         printf("Enter The height in inches: ");
         scanf("%s",new_data->content[2]);
         printf("Enter The width in inches: ");
         scanf("%s",new_data->content[3]);
         printf("Enter The Usage type(Sports/Casuals): ");
         scanf("%s",new_data->content[4]);
         strcpy(new_data->content[5],"END"); // used as a deliminator
         return *new_data;
     }
     product *get_newproduct()
     {
         product *tmp = (product *)malloc(sizeof(product));
         if (tmp == NULL)
         {
             printf("RAM full");
             return NULL;
         }
         return tmp;
     }
     void insert_to_file(product **head,FILE **fileptr)
     {
         *fileptr = fopen("damn.txt","w+");
         fprintf(*fileptr,"%4d\t",ORDERID);
         int i = 0;
         while (strcmp((*head)->details.content[i],"END") != 0)
         {
             fprintf(*fileptr,"%10s\t",(*head)->details.content[i]);
             i++;
         }
     }
     void insertion(product **head,struct data new_item,FILE **fileptr)
     {
         product *iteration = *head;
         product *tmp = get_newproduct();
         int i = 0;
         while (strcmp(new_item.content[i],"END") != 0)
         {
             strcpy(tmp->details.content[i],new_item.content[i]);
             i++;
         }
         tmp->next = NULL;
         if (*head == NULL)
         {
             *head = tmp;
             return;
         }
         while (iteration->next != NULL)
             iteration = iteration->next;
         iteration->next = tmp;
         insert_to_file(head,fileptr);
     }
    
     int main()
     {
         product *new_product = NULL; // initializing is very important!!
         int choice,value;
         FILE *file = NULL;
         struct data new_data;
         while (1)
         {
             printf("=====Enter the gender (1-Boy/Men | 2 for Girl/Women): ====");
             scanf("%d",&value);
             if (value == 1)
                 new_data = init_Tshirt();
             else if (value == 2)
                 new_data = init_Saree();
             else
                 exit(0);
             insertion(&new_product,new_data,&file);
         }
    
         return 0;
     }
    

输入/输出如下:

=====Enter the gender (1-Boy/Men | 2 for Girl/Women): ====1    
We have T-shirt to offer to you !!
Enter the gender (1 for Boy | 2 for Men): 1
Enter The height in inches: 134
Enter The width in inches: 578
Enter The Usage type(Sports/Casuals): Sports
=====Enter the gender (1-Boy/Men | 2 for Girl/Women): ====2    
We have Saree to offer to you !!
Enter the gender (1 for Girl | 2 for Women): 2

文本文件如下:

   3    OrderName: T-shirt  Gender: Boy        134         578      Sports  I_INSTALL_PATH=C:\Program Files\Oracle\VirtualBox\  ogram Files\Oracle\VirtualBox\  irtualBox\    \WINDOWS    M=vscode         ³    rName: Saree     er: Woman  th= 6.5mts          on  176949-sock ASS=c:\Users\krish\AppData\Local\Programs\Microsoft VS Code\resources\app\extensions\git\dist\asÅÔt·    ppData\Local\Programs\Microsoft VS Code\resources\app\extensions\git\dist\asÅÔt·    s\Microsoft VS Code\resources\app\extensions\git\dist\asÅÔt·    resources\app\extensions\git\dis.........................

如何在单独的行中获取所有需要的输出,并在文件中的每一行中获取正确的 ORDERID 值...

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。