user-0 提问: 求助，read函数

回复:
user: user-0
content: 引用 1 楼 自信男孩的回复:#include<stdio.h> #include<stdlib.h> #include<string.h>  struct room {     char state[100];     int price;     int number;     char level[100];     struct room* next; }; struct room *head; struct room *tail; struct room *createlist(); struct room *search(struct room *head); struct room *change(struct room *head,int a); struct room *del(struct room *head,int a); void print(struct room *head);    struct room* createlist()//录入 {     struct room *p;      char state[40];     int price;     int number;     char level[15];      int size=sizeof(struct room);     printf("输入房间编号，等级，价格，状态 注：房间编号为0结束:\n");     scanf("%d",&number);     while(number!=0)     {         //scanf("%s%d%s",&level,&price,&state);         scanf("%s%d%s", level, &price, state);         p=(struct room*)malloc(size);         p->number=number;         strcpy(p->level,level);         p->price=price;         strcpy(p->state,state);         p->next=NULL;         if(head==NULL)             head=p;         else             tail->next=p;         tail=p;         scanf("%d",&number);     }     return head;     system("pause"); }   void print(struct room * head)//显示 {     struct room *ptr;     if(head==NULL)     {         printf("\n no records \n");         return;     }     printf("\n 房间信息为:\n");     printf(" 房间编号\t");     printf(" 房间等级\t");     printf(" 房间价格\t");     printf(" 房间状态\n");     for(ptr=head;ptr!=NULL;ptr=ptr->next)     {         printf("    %d\t          %s\t            %d\t     %s\n",ptr->number,ptr->level,ptr->price,ptr->state);     }     system("pause"); }  struct room *change(struct room *head,int a)//修改 {     struct room *ptr;     char state[40];     int price;     int number;     char level[15];      for(ptr=head;ptr!=NULL;ptr=ptr->next)     {if(ptr->number==a)         {printf("输入房间编号，等级，价格，状态:\n");             //scanf("%d%s%d%s",&number,&level,&price,&state);             scanf("%d%s%d%s",&number, level,&price,state);             ptr->number=number;             strcpy(ptr->level,level);             ptr->price=price;             strcpy(ptr->state,state);}     }     system("pause");     return head; }       struct room *del(struct room *head,int a)//删除 {     struct room *ptr;     for(ptr=head;ptr!=NULL;ptr=ptr->next)     {         //if(ptr->number==a);    //多一个分号         if(ptr->number==a)         {             strcpy(ptr->level,"初始化,请使用修改功能重新录入");             ptr->price=0;             strcpy(ptr->state,"初始化,请使用修改功能重新录入");         }         printf("初始化完成");     }system("pause");     return head; }  struct room *search(struct room *head)//查询 {     int a=0;     struct room *ptr;     printf("\n 空闲房间信息为:\n");     printf(" 房间编号\t");     printf(" 房间等级\t");     printf(" 房间价格\t");     printf(" 房间状态\n");     for(ptr=head;ptr!=NULL;ptr=ptr->next)     {if (strcmp(ptr->state,"空闲")==0)         {a++;             printf("    %d\t          %s\t            %d\t     %s\n",ptr->number,ptr->level,ptr->price,ptr->state);         }}     if(a==0)         printf("当前无空闲房间");     system("pause");     return head; } //读入 struct room * read() {     FILE * fp;     int count=0;     struct room  *head,*p,*q;     head=p=q=NULL;      p=(struct room *)malloc(sizeof(struct room));     head=(struct room *)malloc(sizeof(struct room));      printf("读取中.....\n");      if((fp = fopen("room.txt", "r")) == NULL )     {         printf("载入失败……未找到存档数据!\n\n");         free(p);         free(head);    //不用了就应该释放         return NULL;     }     while(!feof(fp))     {         count++;         p=(struct room *)malloc(sizeof(struct room));         fscanf(fp,"%d %s %d %s\n", &p->number, p->level, &p->price, p->state);         //fread(p,sizeof(struct room),1,fp);          printf("%d %s %d %s \n",p->number,p->level,p->price,p->state);         q->next=p;         q=p;         if(count==1)//标记，第一次循环结束将首地址赋给head；             head=p;     }     fclose(fp);     return head;  }   void write(struct room * head) {     FILE * fp;     struct room * ptr;     fp = fopen("room.txt", "a+");      for(ptr=head;ptr!=NULL;ptr=ptr->next)     {         fprintf(fp,"%d %s %d %s\n",ptr->number,ptr->level,ptr->price,ptr->state);     }      fclose(fp); }    //菜单 void menu() {      int choice=0;     int a=0;     int b=0;      printf("********************************************************************\n");     printf("*      0. 退出系统                                                 *\n");     printf("*      1. 录入信息；*\n");     printf("*      2. 浏览信息；*\n");     printf("*      3. 修改信息;                                                *\n");     printf("*      4. 删除(初始化)信息;                                        *\n");     printf("*      5. 查询信息；*\n");     printf("*      6. 读取信息；*\n");     printf("********************************************************************\n");     printf("\n");       while(1)     { scanf("%d", &choice);         switch (choice) {             case 0:                 write(head);                 printf("退出成功\n");                 system("pause");                 exit(0);                 break;             case 1:                 head=createlist();                 printf("-------录入完成\n");                 break;             case 2:                 printf("-------浏览信息\n");                 print(head);                 break;             case 3:                 printf("------修改信息\n");                  printf("请输入原来的房间编号:");                 scanf("%d", &a);                 change(head, a);                 break;             case 4:                 printf("------删除信息\n");                 printf("请输入删除的房间编号:");                 scanf("%d", &b);                 del(head, b);                 break;             case 5:                 printf("------查询空闲房间\n");                 search(head);                 break;             case 6:                 head=read();                 printf("读取成功");                 break;             default:                 printf("输入错误，重新输入\n");                 system("pause");                 break;         }}     system("cls");  }  int main() {       while (1) {          menu();     }     return 0; } 供参考～  读写不匹配。fscanf/fprintf   fread/fwrite分别是读写对  另外，read函数返回的head和你添加上去的都指向head就会导致数据丢失 其他问题详见代码，建议对比找出自己的问题～ 谢谢大佬，解决了一部分问题，可改完后，只能读取一行数据了，之后的都读取不了了，而且我其实最想问的是，不管改前改后，一调用read，程序就非正常结束了，菜单无法正常使用了

user: user-0
content: 引用 1 楼 自信男孩的回复:#include<stdio.h> #include<stdlib.h> #include<string.h>  struct room {     char state[100];     int price;     int number;     char level[100];     struct room* next; }; struct room *head; struct room *tail; struct room *createlist(); struct room *search(struct room *head); struct room *change(struct room *head,int a); struct room *del(struct room *head,int a); void print(struct room *head);    struct room* createlist()//录入 {     struct room *p;      char state[40];     int price;     int number;     char level[15];      int size=sizeof(struct room);     printf("输入房间编号，等级，价格，状态 注：房间编号为0结束:\n");     scanf("%d",&number);     while(number!=0)     {         //scanf("%s%d%s",&level,&price,&state);         scanf("%s%d%s", level, &price, state);         p=(struct room*)malloc(size);         p->number=number;         strcpy(p->level,level);         p->price=price;         strcpy(p->state,state);         p->next=NULL;         if(head==NULL)             head=p;         else             tail->next=p;         tail=p;         scanf("%d",&number);     }     return head;     system("pause"); }   void print(struct room * head)//显示 {     struct room *ptr;     if(head==NULL)     {         printf("\n no records \n");         return;     }     printf("\n 房间信息为:\n");     printf(" 房间编号\t");     printf(" 房间等级\t");     printf(" 房间价格\t");     printf(" 房间状态\n");     for(ptr=head;ptr!=NULL;ptr=ptr->next)     {         printf("    %d\t          %s\t            %d\t     %s\n",ptr->number,ptr->level,ptr->price,ptr->state);     }     system("pause"); }  struct room *change(struct room *head,int a)//修改 {     struct room *ptr;     char state[40];     int price;     int number;     char level[15];      for(ptr=head;ptr!=NULL;ptr=ptr->next)     {if(ptr->number==a)         {printf("输入房间编号，等级，价格，状态:\n");             //scanf("%d%s%d%s",&number,&level,&price,&state);             scanf("%d%s%d%s",&number, level,&price,state);             ptr->number=number;             strcpy(ptr->level,level);             ptr->price=price;             strcpy(ptr->state,state);}     }     system("pause");     return head; }       struct room *del(struct room *head,int a)//删除 {     struct room *ptr;     for(ptr=head;ptr!=NULL;ptr=ptr->next)     {         //if(ptr->number==a);    //多一个分号         if(ptr->number==a)         {             strcpy(ptr->level,"初始化,请使用修改功能重新录入");             ptr->price=0;             strcpy(ptr->state,"初始化,请使用修改功能重新录入");         }         printf("初始化完成");     }system("pause");     return head; }  struct room *search(struct room *head)//查询 {     int a=0;     struct room *ptr;     printf("\n 空闲房间信息为:\n");     printf(" 房间编号\t");     printf(" 房间等级\t");     printf(" 房间价格\t");     printf(" 房间状态\n");     for(ptr=head;ptr!=NULL;ptr=ptr->next)     {if (strcmp(ptr->state,"空闲")==0)         {a++;             printf("    %d\t          %s\t            %d\t     %s\n",ptr->number,ptr->level,ptr->price,ptr->state);         }}     if(a==0)         printf("当前无空闲房间");     system("pause");     return head; } //读入 struct room * read() {     FILE * fp;     int count=0;     struct room  *head,*p,*q;     head=p=q=NULL;      p=(struct room *)malloc(sizeof(struct room));     head=(struct room *)malloc(sizeof(struct room));      printf("读取中.....\n");      if((fp = fopen("room.txt", "r")) == NULL )     {         printf("载入失败……未找到存档数据!\n\n");         free(p);         free(head);    //不用了就应该释放         return NULL;     }     while(!feof(fp))     {         count++;         p=(struct room *)malloc(sizeof(struct room));         fscanf(fp,"%d %s %d %s\n", &p->number, p->level, &p->price, p->state);         //fread(p,sizeof(struct room),1,fp);          printf("%d %s %d %s \n",p->number,p->level,p->price,p->state);         q->next=p;         q=p;         if(count==1)//标记，第一次循环结束将首地址赋给head；             head=p;     }     fclose(fp);     return head;  }   void write(struct room * head) {     FILE * fp;     struct room * ptr;     fp = fopen("room.txt", "a+");      for(ptr=head;ptr!=NULL;ptr=ptr->next)     {         fprintf(fp,"%d %s %d %s\n",ptr->number,ptr->level,ptr->price,ptr->state);     }      fclose(fp); }    //菜单 void menu() {      int choice=0;     int a=0;     int b=0;      printf("********************************************************************\n");     printf("*      0. 退出系统                                                 *\n");     printf("*      1. 录入信息；*\n");     printf("*      2. 浏览信息；*\n");     printf("*      3. 修改信息;                                                *\n");     printf("*      4. 删除(初始化)信息;                                        *\n");     printf("*      5. 查询信息；*\n");     printf("*      6. 读取信息；*\n");     printf("********************************************************************\n");     printf("\n");       while(1)     { scanf("%d", &choice);         switch (choice) {             case 0:                 write(head);                 printf("退出成功\n");                 system("pause");                 exit(0);                 break;             case 1:                 head=createlist();                 printf("-------录入完成\n");                 break;             case 2:                 printf("-------浏览信息\n");                 print(head);                 break;             case 3:                 printf("------修改信息\n");                  printf("请输入原来的房间编号:");                 scanf("%d", &a);                 change(head, a);                 break;             case 4:                 printf("------删除信息\n");                 printf("请输入删除的房间编号:");                 scanf("%d", &b);                 del(head, b);                 break;             case 5:                 printf("------查询空闲房间\n");                 search(head);                 break;             case 6:                 head=read();                 printf("读取成功");                 break;             default:                 printf("输入错误，重新输入\n");                 system("pause");                 break;         }}     system("cls");  }  int main() {       while (1) {          menu();     }     return 0; } 供参考～  读写不匹配。fscanf/fprintf   fread/fwrite分别是读写对  另外，read函数返回的head和你添加上去的都指向head就会导致数据丢失 其他问题详见代码，建议对比找出自己的问题～ 谢谢大佬，解决了一部分问题，可改完后，只能读取一行数据了，之后的都读取不了了，而且我其实最想问的是，不管改前改后，一调用read，程序就非正常结束了，菜单无法正常使用了

user: user-1
content: 推荐读一下 https://blog.csdn.net/zhanghaiyang9999/article/details/107032563

user: user-2
content: #include<stdio.h> #include<stdlib.h> #include<string.h>  struct room {     char state[100];     int price;     int number;     char level[100];     struct room* next; }; struct room *head; struct room *tail; struct room *createlist(); struct room *search(struct room *head); struct room *change(struct room *head,int a); struct room *del(struct room *head,int a); void print(struct room *head);    struct room* createlist()//录入 {     struct room *p;      char state[40];     int price;     int number;     char level[15];      int size=sizeof(struct room);     printf("输入房间编号，等级，价格，状态 注：房间编号为0结束:\n");     scanf("%d",&number);     while(number!=0)     {         //scanf("%s%d%s",&level,&price,&state);         scanf("%s%d%s", level, &price, state);         p=(struct room*)malloc(size);         p->number=number;         strcpy(p->level,level);         p->price=price;         strcpy(p->state,state);         p->next=NULL;         if(head==NULL)             head=p;         else             tail->next=p;         tail=p;         scanf("%d",&number);     }     return head;     system("pause"); }   void print(struct room * head)//显示 {     struct room *ptr;     if(head==NULL)     {         printf("\n no records \n");         return;     }     printf("\n 房间信息为:\n");     printf(" 房间编号\t");     printf(" 房间等级\t");     printf(" 房间价格\t");     printf(" 房间状态\n");     for(ptr=head;ptr!=NULL;ptr=ptr->next)     {         printf("    %d\t          %s\t            %d\t     %s\n",ptr->number,ptr->level,ptr->price,ptr->state);     }     system("pause"); }  struct room *change(struct room *head,int a)//修改 {     struct room *ptr;     char state[40];     int price;     int number;     char level[15];      for(ptr=head;ptr!=NULL;ptr=ptr->next)     {if(ptr->number==a)         {printf("输入房间编号，等级，价格，状态:\n");             //scanf("%d%s%d%s",&number,&level,&price,&state);             scanf("%d%s%d%s",&number, level,&price,state);             ptr->number=number;             strcpy(ptr->level,level);             ptr->price=price;             strcpy(ptr->state,state);}     }     system("pause");     return head; }       struct room *del(struct room *head,int a)//删除 {     struct room *ptr;     for(ptr=head;ptr!=NULL;ptr=ptr->next)     {         //if(ptr->number==a);    //多一个分号         if(ptr->number==a)         {             strcpy(ptr->level,"初始化,请使用修改功能重新录入");             ptr->price=0;             strcpy(ptr->state,"初始化,请使用修改功能重新录入");         }         printf("初始化完成");     }system("pause");     return head; }  struct room *search(struct room *head)//查询 {     int a=0;     struct room *ptr;     printf("\n 空闲房间信息为:\n");     printf(" 房间编号\t");     printf(" 房间等级\t");     printf(" 房间价格\t");     printf(" 房间状态\n");     for(ptr=head;ptr!=NULL;ptr=ptr->next)     {if (strcmp(ptr->state,"空闲")==0)         {a++;             printf("    %d\t          %s\t            %d\t     %s\n",ptr->number,ptr->level,ptr->price,ptr->state);         }}     if(a==0)         printf("当前无空闲房间");     system("pause");     return head; } //读入 struct room * read() {     FILE * fp;     int count=0;     struct room  *head,*p,*q;     head=p=q=NULL;      p=(struct room *)malloc(sizeof(struct room));     head=(struct room *)malloc(sizeof(struct room));      printf("读取中.....\n");      if((fp = fopen("room.txt", "r")) == NULL )     {         printf("载入失败……未找到存档数据!\n\n");         free(p);         free(head);    //不用了就应该释放         return NULL;     }     while(!feof(fp))     {         count++;         p=(struct room *)malloc(sizeof(struct room));         fscanf(fp,"%d %s %d %s\n", &p->number, p->level, &p->price, p->state);         //fread(p,sizeof(struct room),1,fp);          printf("%d %s %d %s \n",p->number,p->level,p->price,p->state);         q->next=p;         q=p;         if(count==1)//标记，第一次循环结束将首地址赋给head；             head=p;     }     fclose(fp);     return head;  }   void write(struct room * head) {     FILE * fp;     struct room * ptr;     fp = fopen("room.txt", "a+");      for(ptr=head;ptr!=NULL;ptr=ptr->next)     {         fprintf(fp,"%d %s %d %s\n",ptr->number,ptr->level,ptr->price,ptr->state);     }      fclose(fp); }    //菜单 void menu() {      int choice=0;     int a=0;     int b=0;      printf("********************************************************************\n");     printf("*      0. 退出系统                                                 *\n");     printf("*      1. 录入信息；*\n");     printf("*      2. 浏览信息；*\n");     printf("*      3. 修改信息;                                                *\n");     printf("*      4. 删除(初始化)信息;                                        *\n");     printf("*      5. 查询信息；*\n");     printf("*      6. 读取信息；*\n");     printf("********************************************************************\n");     printf("\n");       while(1)     { scanf("%d", &choice);         switch (choice) {             case 0:                 write(head);                 printf("退出成功\n");                 system("pause");                 exit(0);                 break;             case 1:                 head=createlist();                 printf("-------录入完成\n");                 break;             case 2:                 printf("-------浏览信息\n");                 print(head);                 break;             case 3:                 printf("------修改信息\n");                  printf("请输入原来的房间编号:");                 scanf("%d", &a);                 change(head, a);                 break;             case 4:                 printf("------删除信息\n");                 printf("请输入删除的房间编号:");                 scanf("%d", &b);                 del(head, b);                 break;             case 5:                 printf("------查询空闲房间\n");                 search(head);                 break;             case 6:                 head=read();                 printf("读取成功");                 break;             default:                 printf("输入错误，重新输入\n");                 system("pause");                 break;         }}     system("cls");  }  int main() {       while (1) {          menu();     }     return 0; } 供参考～  读写不匹配。fscanf/fprintf   fread/fwrite分别是读写对  另外，read函数返回的head和你添加上去的都指向head就会导致数据丢失 其他问题详见代码，建议对比找出自己的问题～