user-0 提问: 第三次求助这个问题了

回复:
user: user-1
content: 把菜单里case0里的write(head)删除。关闭后再运行没出现你说的情况，除了调用write(head)函数换了个地方，其他都是你的代码没有变

user: user-0
content: 引用 7 楼 chxchxkkk的回复: 把菜单里case0里的write(head)删除。关闭后再运行没出现你说的情况，除了调用write(head)函数换了个地方，其他都是你的代码没有变 大佬 能直接发下源代码吗，万分感谢

user: user-1
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);         p=(struct room*)malloc(sizeof(struct room));         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);     }     write(head);     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);             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)         {             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;       q=(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);          printf("%d %s %d %s \n",p->number,p->level,p->price,p->state);         q->next=p;         q=p;         if(count==1)             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; }

user: user-0
content: 引用 5 楼 chxchxkkk 的回复:并没有出现你的情况。  但是你的菜单里只有选择退出才会写入文件，这里就会出现不退出发直接选择6读取文件，但这个文件并不存在， 把write(head)放到录入函数中         write(head);     return head; 结果是这样的：  是我表达能力的问题，大佬，我的意思是，关闭界面，再一次打开界面，读取就出问题了，这是第一次， 跟大佬一样，没什么问题，但关闭界面后，第二次打开，再读取后功能2，显示就出现了屯屯屯乱码，  #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); //写入 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); }     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);         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);     } 	write(head);     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(" 房间状态\t");      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);             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)         {             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(" 房间状态\t");     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; 	struct room *head, *p, *q;  	if ((fp = fopen("room.txt", "r")) == NULL) { 		printf("载入失败，未找到存档数据!\n\n"); 		return NULL; 	}  	q = head = (struct room *)malloc(sizeof(struct room)); 	head->next = NULL; 	printf("读取中.....\n");  	while (!feof(fp)) { 		p = (struct room *)malloc(sizeof(struct room)); 		fscanf(fp, "%d %s %d %s\n", &p->number, p->level, &p->price, p->state); 		p->next = NULL; 		printf("%d %s %d %s \n", p->number, p->level, p->price, p->state);  		q->next = p; 		q = p; 	} 	fclose(fp); 	return head; }     //菜单 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:                  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; }

user: user-1
content: 并没有出现你的情况。  但是你的菜单里只有选择退出才会写入文件，这里就会出现不退出发直接选择6读取文件，但这个文件并不存在， 把write(head)放到录入函数中         write(head);     return head; 结果是这样的：

user: user-0
content: 引用 3 楼 dalaobangmang的回复:[quote=引用 1 楼 user-2 的回复:] [code=c] //读入 struct room *read() { 	FILE *fp; 	struct room *head, *p, *q;  	if ((fp = fopen("room.txt", "r")) == NULL) { 		printf("载入失败……未找到存档数据!\n\n"); 		return NULL; 	}  	q = head = (struct room *)malloc(sizeof(struct room)); 	head->next = NULL; 	printf("读取中.....\n");  	while (!feof(fp)) { 		p = (struct room *)malloc(sizeof(struct room)); 		fscanf(fp, "%d %s %d %s\n", &p->number, p->level, &p->price, p->state); 		p->next = NULL; 		printf("%d %s %d %s \n", p->number, p->level, p->price, p->state);  		q->next = p; 		q = p; 	} 	fclose(fp); 	return head; }    读取还出错了，如图 第二张是保存退出后的文件变化

user: user-0
content: [quote=引用 1 楼 user-2 的回复:] [code=c] //读入 struct room *read() { 	FILE *fp; 	struct room *head, *p, *q;  	if ((fp = fopen("room.txt", "r")) == NULL) { 		printf("载入失败……未找到存档数据!\n\n"); 		return NULL; 	}  	q = head = (struct room *)malloc(sizeof(struct room)); 	head->next = NULL; 	printf("读取中.....\n");  	while (!feof(fp)) { 		p = (struct room *)malloc(sizeof(struct room)); 		fscanf(fp, "%d %s %d %s\n", &p->number, p->level, &p->price, p->state); 		p->next = NULL; 		printf("%d %s %d %s \n", p->number, p->level, p->price, p->state);  		q->next = p; 		q = p; 	} 	fclose(fp); 	return head; }    读取还出错了，如图

user: user-0
content: 引用 1 楼 user-2的回复: //读入 struct room *read() { 	FILE *fp; 	struct room *head, *p, *q;  	if ((fp = fopen("room.txt", "r")) == NULL) { 		printf("载入失败……未找到存档数据!\n\n"); 		return NULL; 	}  	q = head = (struct room *)malloc(sizeof(struct room)); 	head->next = NULL; 	printf("读取中.....\n");  	while (!feof(fp)) { 		p = (struct room *)malloc(sizeof(struct room)); 		fscanf(fp, "%d %s %d %s\n", &p->number, p->level, &p->price, p->state); 		p->next = NULL; 		printf("%d %s %d %s \n", p->number, p->level, p->price, p->state);  		q->next = p; 		q = p; 	} 	fclose(fp); 	return head; }   运行结果没有变化(>﹏<)

user: user-2
content: //读入 struct room *read() { 	FILE *fp; 	struct room *head, *p, *q;  	if ((fp = fopen("room.txt", "r")) == NULL) { 		printf("载入失败……未找到存档数据!\n\n"); 		return NULL; 	}  	q = head = (struct room *)malloc(sizeof(struct room)); 	head->next = NULL; 	printf("读取中.....\n");  	while (!feof(fp)) { 		p = (struct room *)malloc(sizeof(struct room)); 		fscanf(fp, "%d %s %d %s\n", &p->number, p->level, &p->price, p->state); 		p->next = NULL; 		printf("%d %s %d %s \n", p->number, p->level, p->price, p->state);  		q->next = p; 		q = p; 	} 	fclose(fp); 	return head; }