用fflush(stdin)清楚缓冲会把我从文件中调用的head结构体指针清空了,
导致出错,fflush(stdin)换成getchar(),也是一样的问题。
大家可以用output函数检查结果。请高手帮我解决一下这个问题,谢谢了。
这个程序有点长,劳烦大家浪费时间了,但有错误的已经在下面标明已经证实此程序在tc2.0环境下
运行不会出现编译错误。再次感谢。
#include<stdio.h>
#include<malloc.h>
#include <string.h>
#include <stdlib.h>
#define NULL 0
#define len sizeof(struct student)
struct student
{
long num;
char name[20];
float msco;
float csco;
float esco;
struct student *next;
};
int n;
struct student *creat()
{
struct student *head;
struct student *p1,*p2;
n=0;
do
{
p1=(struct student *)malloc(len);
printf("please input the number of the student:");
scanf("%ld",&p1->num);
if(p1->num==0)break;
printf("please input the name of the student:");
scanf("%s",&p1->name);
printf("please input the math of the student:");
scanf("%f",&p1->msco);
printf("please input the cprogram of the student:");
scanf("%f",&p1->csco);
printf("please input the english of the student:");
scanf("%f",&p1->esco);
n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
}while(p1->num!=0);
p2->next=NULL;
return(head);
}
struct student *del(struct student *head)
{
char stv;
struct student *p1,*p2;
struct student *save( struct student *head);
struct student *load();
long num;
printf("input the number you want delete:");
scanf("%ld",&num);
if(head==NULL)
{
printf("\nlist is null!");
goto end;
}
p1=head;
if(num==p1->num)
{
head=p1->next;
}
else
{
while(num!=p1->num&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(num!=p1->num&&p1->next==NULL)
{
printf("cannot find the number!\n");
}
else
{
p2->next=p1->next;
}
}
n=n-1;
end:;
fflush(stdin); /*清空了前面删除得到的结构体指针head*/
printf("would you save it?(y/n)");
scanf("%c,&stv"); /* 这部分个人觉得应该是保存了一个空的结构体指针head*/
if(stv=='y')
save(head);
else if(stv=='n')
return(head);
}
struct student *save( struct student *head)
{
FILE *fp;
struct student *p;
fp=fopen("stu_list","wb");
p=head;
if(head!=NULL)
do
{
fwrite(p,len,1,fp);
p=p->next;
}while(p!=NULL);
fclose(fp);
}
struct student *load()
{
FILE *fp;
struct student *p1,*p2,*head=NULL;
fp=fopen("stu_list","rb");
p1=(struct student *)malloc(len);
head=p1;
head=p1;
while(!feof(fp))
{
if(fread(p1,len,1,fp)!=1) break;
p1->next=(struct student *)malloc(len);
if(!p1->next)
{
printf("Out of memory!\n");
return (head);
}
p2=p1;
p1=p1->next;
}
p2->next=NULL;
fclose(fp);
printf("\nYou have success to read data from the file!\n");
return (head);
}
void print(struct student *head)
{
struct student *p;
p=head;
if(head!=NULL)
do
{
printf("\n");
printf("%d",p->num);
printf("%7s",p->name);
printf("%7.2f",p->msco);
printf("%7.2f",p->csco);
printf("%7.2f",p->esco);
p=p->next;
}while(p!=NULL);
}
main()
{
char ate;
float arg,*point=&arg;
struct student *head;
struct student *p;
int hj;
system("cls");
l1:printf("\n1.input the information\n");
printf("2.delete the information\n");
printf("3.save the information\n");
printf("4.load the information\n");
printf("5.search the information\n");
printf("6.insert the information\n");
printf("7.outout the information\n");
printf("8.sort the information\n");
printf("0.quit the program.\n");
scanf("%d",&hj);
switch(hj)
{
case 1:{system("cls");head=creat();}break;
case 2:{system("cls");head=del(head);}break;
case 3:{system("cls");save(head);}break;
case 4:{system("cls");head=load();}break;
case 5:
case 7:{system("cls");print(head);}break;
case 0:
{
fflush(stdin); /* fflush清空了结构体指针,导致最后的保存出现错误*/
printf("would you save you information?(y/n)"); /*输入y保存并退出,输入n不保存退出*/
scanf("%c",&ate);
if(ate=='y')
{
save(head);
goto end;
}
else if(ate=='n')
goto end;
}
}
goto l1;
end:;
}
7 个解决方案
#1
struct student *save( struct student *head)
{
FILE *fp;
struct student *p;
fp=fopen("stu_list","wb");
p=head;
if(head!=NULL)
do
{
fwrite(p,len,1,fp);
p=p->next;
}while(p!=NULL);
fclose(fp);
}
这个函数没有返回值,
而你在DEL函数里面写了
printf("would you save it?(y/n)");
scanf("%c,&stv"); /* 这部分个人觉得应该是保存了一个空的结构体指针head*/
if(stv=='y')
save(head);//若 if(stv=='y')成立,整个函数没有返回值了也
else if(stv=='n')
return(head);
#2
#3
/*你的错误不严重我已经帮你改好了
只要自己认真点仔细点就没问题了
scanf("%c,&stv"); /* 这部分个人觉得应该是保存了一个空的结构体指针head*/
struct student *head;
你仔细看就知道问题在那了
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <stdlib.h>
#define NULL 0
#define len sizeof(struct student)
struct student
{
long num;
char name[20];
float msco;
float csco;
float esco;
struct student *next;
};
int n;
struct student *creat()
{
struct student *head;
struct student *p1,*p2;
n=0;
do
{
p1=(struct student *)malloc(len);
printf("please input the number of the student:");
scanf("%ld",&p1->num);
if(p1->num==0)break;
printf("please input the name of the student:");
scanf("%s",&p1->name);
printf("please input the math of the student:");
scanf("%f",&p1->msco);
printf("please input the cprogram of the student:");
scanf("%f",&p1->csco);
printf("please input the english of the student:");
scanf("%f",&p1->esco);
n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
}while(p1->num!=0);
p2->next=NULL;
return(head);
}
struct student *del(struct student *head)
{
char stv;
struct student *p1,*p2;
struct student *save( struct student *head);
struct student *load();
long num;
printf("input the number you want delete:");
scanf("%ld",&num);
if(head==NULL)
{
printf("\nlist is null!");
goto end;
}
p1=head;
if(num==p1->num)
{
head=p1->next;
}
else
{
while(num!=p1->num&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(num!=p1->num&&p1->next==NULL)
{
printf("cannot find the number!\n");
}
else
{
p2->next=p1->next;
}
}
n=n-1;
end:;
fflush(stdin); /*清空了前面删除得到的结构体指针head*/
printf("would you save it?(y/n)");
scanf("%c",&stv); /* 这部分个人觉得应该是保存了一个空的结构体指针head*/
if(stv=='y')
save(head);
else if(stv=='n')
return(head);
}
struct student *save( struct student *head)
{
FILE *fp;
struct student *p;
fp=fopen("stu_list","wb");
p=head;
if(head!=NULL)
do
{
fwrite(p,len,1,fp);
p=p->next;
}while(p!=NULL);
fclose(fp);
return 0;
}
struct student *load()
{
FILE *fp;
struct student *p1,*p2,*head=NULL;
fp=fopen("stu_list","rb");
p1=(struct student *)malloc(len);
head=p1;
head=p1;
while(!feof(fp))
{
if(fread(p1,len,1,fp)!=1) break;
p1->next=(struct student *)malloc(len);
if(!p1->next)
{
printf("Out of memory!\n");
return (head);
}
p2=p1;
p1=p1->next;
}
p2->next=NULL;
fclose(fp);
printf("\nYou have success to read data from the file!\n");
return (head);
}
void print(struct student *head)
{
struct student *p;
p=head;
if(head!=NULL)
do
{
printf("\n");
printf("%d",p->num);
printf("%7s",p->name);
printf("%7.2f",p->msco);
printf("%7.2f",p->csco);
printf("%7.2f",p->esco);
p=p->next;
}while(p!=NULL);
}
int main()
{
char ate;
float arg,*point=&arg;
struct student *head = NULL;
struct student *p;
int hj;
system("cls");
l1:printf("\n1.input the information\n");
printf("2.delete the information\n");
printf("3.save the information\n");
printf("4.load the information\n");
printf("5.search the information\n");
printf("6.insert the information\n");
printf("7.outout the information\n");
printf("8.sort the information\n");
printf("0.quit the program.\n");
scanf("%d",&hj);
switch(hj)
{
case 1:{system("cls");head=creat();}break;
case 2:{system("cls");head=del(head);}break;
case 3:{system("cls");save(head);}break;
case 4:{system("cls");head=load();}break;
case 5:
case 7:{system("cls");print(head);}break;
case 0:
{
fflush(stdin); /* fflush清空了结构体指针,导致最后的保存出现错误*/
printf("would you save you information?(y/n)"); /*输入y保存并退出,输入n不保存退出*/
scanf("%c",&ate);
if(ate=='y')
{
save(head);
goto end;
}
else if(ate=='n')
goto end;
}
}
goto l1;
end:;
}
#4
在主函数
l1:printf("\n1.input the information\n");
printf("2.delete the information\n");
printf("3.save the information\n");
printf("4.load the information\n");
printf("5.search the information\n");
printf("6.insert the information\n");
printf("7.outout the information\n");
printf("8.sort the information\n");
printf("0.quit the program.\n");
scanf("%d",&hj);
fflush(stdin);//加一句容错
l1:printf("\n1.input the information\n");
printf("2.delete the information\n");
printf("3.save the information\n");
printf("4.load the information\n");
printf("5.search the information\n");
printf("6.insert the information\n");
printf("7.outout the information\n");
printf("8.sort the information\n");
printf("0.quit the program.\n");
scanf("%d",&hj);
fflush(stdin);//加一句容错
#5
http://www.pceggs.com/PgADPoint.aspx?UserID=7507913
#6
没装TC!~up
#7
scanf(" %c",&ate);
这样就行了 删掉 fflush
这样就行了 删掉 fflush
#1
struct student *save( struct student *head)
{
FILE *fp;
struct student *p;
fp=fopen("stu_list","wb");
p=head;
if(head!=NULL)
do
{
fwrite(p,len,1,fp);
p=p->next;
}while(p!=NULL);
fclose(fp);
}
这个函数没有返回值,
而你在DEL函数里面写了
printf("would you save it?(y/n)");
scanf("%c,&stv"); /* 这部分个人觉得应该是保存了一个空的结构体指针head*/
if(stv=='y')
save(head);//若 if(stv=='y')成立,整个函数没有返回值了也
else if(stv=='n')
return(head);
#2
#3
/*你的错误不严重我已经帮你改好了
只要自己认真点仔细点就没问题了
scanf("%c,&stv"); /* 这部分个人觉得应该是保存了一个空的结构体指针head*/
struct student *head;
你仔细看就知道问题在那了
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <stdlib.h>
#define NULL 0
#define len sizeof(struct student)
struct student
{
long num;
char name[20];
float msco;
float csco;
float esco;
struct student *next;
};
int n;
struct student *creat()
{
struct student *head;
struct student *p1,*p2;
n=0;
do
{
p1=(struct student *)malloc(len);
printf("please input the number of the student:");
scanf("%ld",&p1->num);
if(p1->num==0)break;
printf("please input the name of the student:");
scanf("%s",&p1->name);
printf("please input the math of the student:");
scanf("%f",&p1->msco);
printf("please input the cprogram of the student:");
scanf("%f",&p1->csco);
printf("please input the english of the student:");
scanf("%f",&p1->esco);
n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
}while(p1->num!=0);
p2->next=NULL;
return(head);
}
struct student *del(struct student *head)
{
char stv;
struct student *p1,*p2;
struct student *save( struct student *head);
struct student *load();
long num;
printf("input the number you want delete:");
scanf("%ld",&num);
if(head==NULL)
{
printf("\nlist is null!");
goto end;
}
p1=head;
if(num==p1->num)
{
head=p1->next;
}
else
{
while(num!=p1->num&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(num!=p1->num&&p1->next==NULL)
{
printf("cannot find the number!\n");
}
else
{
p2->next=p1->next;
}
}
n=n-1;
end:;
fflush(stdin); /*清空了前面删除得到的结构体指针head*/
printf("would you save it?(y/n)");
scanf("%c",&stv); /* 这部分个人觉得应该是保存了一个空的结构体指针head*/
if(stv=='y')
save(head);
else if(stv=='n')
return(head);
}
struct student *save( struct student *head)
{
FILE *fp;
struct student *p;
fp=fopen("stu_list","wb");
p=head;
if(head!=NULL)
do
{
fwrite(p,len,1,fp);
p=p->next;
}while(p!=NULL);
fclose(fp);
return 0;
}
struct student *load()
{
FILE *fp;
struct student *p1,*p2,*head=NULL;
fp=fopen("stu_list","rb");
p1=(struct student *)malloc(len);
head=p1;
head=p1;
while(!feof(fp))
{
if(fread(p1,len,1,fp)!=1) break;
p1->next=(struct student *)malloc(len);
if(!p1->next)
{
printf("Out of memory!\n");
return (head);
}
p2=p1;
p1=p1->next;
}
p2->next=NULL;
fclose(fp);
printf("\nYou have success to read data from the file!\n");
return (head);
}
void print(struct student *head)
{
struct student *p;
p=head;
if(head!=NULL)
do
{
printf("\n");
printf("%d",p->num);
printf("%7s",p->name);
printf("%7.2f",p->msco);
printf("%7.2f",p->csco);
printf("%7.2f",p->esco);
p=p->next;
}while(p!=NULL);
}
int main()
{
char ate;
float arg,*point=&arg;
struct student *head = NULL;
struct student *p;
int hj;
system("cls");
l1:printf("\n1.input the information\n");
printf("2.delete the information\n");
printf("3.save the information\n");
printf("4.load the information\n");
printf("5.search the information\n");
printf("6.insert the information\n");
printf("7.outout the information\n");
printf("8.sort the information\n");
printf("0.quit the program.\n");
scanf("%d",&hj);
switch(hj)
{
case 1:{system("cls");head=creat();}break;
case 2:{system("cls");head=del(head);}break;
case 3:{system("cls");save(head);}break;
case 4:{system("cls");head=load();}break;
case 5:
case 7:{system("cls");print(head);}break;
case 0:
{
fflush(stdin); /* fflush清空了结构体指针,导致最后的保存出现错误*/
printf("would you save you information?(y/n)"); /*输入y保存并退出,输入n不保存退出*/
scanf("%c",&ate);
if(ate=='y')
{
save(head);
goto end;
}
else if(ate=='n')
goto end;
}
}
goto l1;
end:;
}
#4
在主函数
l1:printf("\n1.input the information\n");
printf("2.delete the information\n");
printf("3.save the information\n");
printf("4.load the information\n");
printf("5.search the information\n");
printf("6.insert the information\n");
printf("7.outout the information\n");
printf("8.sort the information\n");
printf("0.quit the program.\n");
scanf("%d",&hj);
fflush(stdin);//加一句容错
l1:printf("\n1.input the information\n");
printf("2.delete the information\n");
printf("3.save the information\n");
printf("4.load the information\n");
printf("5.search the information\n");
printf("6.insert the information\n");
printf("7.outout the information\n");
printf("8.sort the information\n");
printf("0.quit the program.\n");
scanf("%d",&hj);
fflush(stdin);//加一句容错
#5
http://www.pceggs.com/PgADPoint.aspx?UserID=7507913
#6
没装TC!~up
#7
scanf(" %c",&ate);
这样就行了 删掉 fflush
这样就行了 删掉 fflush