【帮忙查错】我看了很多遍,测试了很多遍,还是没正确。
#include<stdio.h>#include<string.h>
#include<stdlib.h>
#include<malloc.h>
#define LEN sizeof(struct student)
struct student
{
char name[20];
char phone[20];
struct student *next;
};
void face(void)
{
printf("1,addend\n2,dispaly\n3,delete\n4,save\n5,exit\n");
}
void print(struct student *head)
{
struct student *p;
p=head;
printf("the list\n");
if(head!=NULL)
do
{
printf("name:%s\n",p->name);
printf("phone:%s\n",p->phone);
p=p->next;
}
while(p!=NULL);
else
{
printf("NO ONE\n");
}
}
struct student *append(struct student *head)
{
struct student *p0=NULL;
printf("input the address information:\n");
p0=(struct student *)malloc(LEN);
printf("input name:\n");
scanf("%s",p0->name);
printf("input phone:\n");
scanf("%s",p0->phone);
printf("%s\n",p0->name);
printf("you addend is succs\n");
return head;
}
struct student *del(struct student *head)
{
struct student *p1,*p2;
char name[12];
printf("input del information:\n");
scanf("%s\n",name);
p1 = head;
if(head == NULL)
{
printf("NO information.\n");
return(head);
}
while(p1!=NULL)
{
if(strcmp(p1->name,name)==0)
{
if(p1==head)
head=p1->next;
else
p2->next=p1->next;
printf("del done\n");
return(head);
}
p2=p1;
p1=p1->next;
}
printf("no information\n");
return head;
}
int save(struct student *head)//估计错在这里,addend的函数没有传进来,这么改都改不了。
{
FILE *fp,*fp1;
struct student *p;
p=head;
fp=fopen("address.txt","w+");
fp1=fopen("address1.txt","w+");
fprintf(fp1,"information list\n");
//printf("%s,%s",p->name,p->phone);
while(p!=NULL)
{
//fprintf(fp,"%s,%s",p->name,p->phone);
printf("%s\n",p->name);
printf("%s\n",p->phone);
fprintf(fp1,"name:%s\n",p->name);
fprintf(fp1,"phone:%s\n",p->phone);
p=p->next;
}
if(p==NULL)
{printf("this is NULL.\n");}
fclose(fp1);
fclose(fp);
printf("save done\n");
exit(1);
//return 0;
}
struct student *load(void)
{
FILE *fp;
struct student *head=NULL,*p1=NULL,*p2=NULL;
char c;
int i;
fp=fopen("address.txt","r");
for(i=1;(c=fgetc(fp))!=-1;i++)
{
p1=(struct student *)malloc(LEN);
if(i==1)
{
head=p1;p2=p1;
}
else
{
p2->next=p1;p2=p1;
}
}
if(p1==NULL)
{fclose(fp);return(head);}
p2->next=NULL;
fclose(fp);
return(head);
}
int main()
{
FILE *fp1,*fp2;
int c=0;
struct student *head=NULL;
if((fp1=fopen("address.txt","r"))==NULL);
{
fp2=fopen("address.txt","w");
fclose(fp2);
}
head=load();
while(1)
{
face();
//int c=0;
printf("choice\n");
scanf("%d",&c);
switch(c)
{
case 1:head=append(head);break;
//save(qhead);break;
case 2:print(head);break;
case 3:head=del(head);break;
case 4:save(head);break;
case 5:exit(0);break;
default:printf("Enter error\n");break;
}
printf("Enter\n");
//face();
}
return 0;
}