帮忙看一下这代码,问题出在哪了?要求是从文件导入,然后进行相应的操作
#include<stdio.h>#include<stdlib.h>
struct student
{
int num;
char name[10];
struct student *next;
};
int x;
void Add();
void Delete();
void Modify();
void Query();
struct student *pnew,*tail,*head,*p;
int main()
{
FILE*fp1;
FILE*fp2;
p=(struct student*)malloc(sizeof(struct student));
if((fp1=fopen("C:\\Users\\dell\\Desktop\\1.txt","r"))==NULL)
{
printf("打开错误");exit(0);
}
if((fp2=fopen("C:\\Users\\dell\\Desktop\\2.txt","w"))==NULL)
{
printf("打开错误");exit(0);
}
while((fscanf(fp1,"%d %s",&p->num,p->name))!=EOF)
{
printf("%d %s\n",p->num,p->name);
}printf("初始化.........\n");
char ch[6];
scanf("%s",&ch);
while(ch[0]!='E')
{
switch(ch[0])
{
case 'A':Add();break;
case 'D':Delete();break;
case 'M':Modify();break;
case 'Q':Query();break;
}
}
if(ch[0]=='E')
{
fprintf(fp2,"%d %s",pnew->num,pnew->name);
fclose(fp1);
fclose(fp2);
}
return 0;
}
void Add()
{
printf("添加信息:");
pnew=(struct student*)malloc(sizeof(struct student));
pnew->next=NULL;
scanf("%d %s",&pnew->num,pnew->name);
if(head==NULL)
{head=pnew;return ;}
else
tail->next=pnew;
tail=pnew;
}
void Delete()
{
struct student *h,*d;
printf("输入要删除的学号");
scanf("%d",&x);
if(head->num==x)
{
head=head->next;
}
d=h=head;
while(h->next!=NULL)
{
if(h->num==x)
{
d->next=h->next;
}
d=h;
h=h->next;
}
printf(" 输入错误,请重新输入");
Delete();
}
void Modify()
{
struct student *H;
printf("输入要修改的学生学号");
scanf("%d",&x);
H=head;
while(H!=NULL)
{
if(H->num==x)
{
printf("请输入修改信息");
scanf("%d %s",&H->num,H->name);
}
H=H->next;
}
printf("学生信息不存在,重新输入");
Modify();
}
void Query()
{
struct student *H;
H=head;
printf("输入要查询的学生学号");
scanf("%d",&x);
while(H!=NULL)
{
if(H->num==x)
printf("%d &s",H->num,H->name);
}
printf("学生信息不存在,重新输入");
Query();
}