没有错误信息,就是存取功能错误,大神教教

蔡文豪 发布于 2013/05/28 17:21
阅读 76
收藏 0
#include<stdio.h>
#include<malloc.h>
struct worker
{
char name[20];
long num;
struct worker *next;
};
typedef  struct worker wor;
int read(wor **head)
{
FILE *fp;
wor *s,*p;
int n;
fp=fopen("c:\\2.txt","ab+");//ÒÔÖƶȷ½Ê½´ò¿ªÒ»¸ö¶þ½øÖÆÎļþ
if(NULL==fp)//Èç¹û´ò¿ªÊ§°Ü¾Í·µ»Ø-1£»
{
printf("´ò¿ªÎļþʧ°Ü£¡\n");
*head=NULL;
return -1;
}
*head=(wor *)malloc(sizeof(wor));
p=*head;
while(!feof(fp))
{//Ϊϸö½Úµã·ÖÅäÄÚ´æ¿Õ¼ä
s=(wor *)malloc(sizeof(wor));
n=fread(s,sizeof(wor),1,fp);//´ÓÎļþ¶ÁÈ¡Êý¾Ýµ½½Úµã
if(n<1)
{
free(s);
p->next=NULL;//×îºóÒ»¸ö½ÚµãΪNULL
break;
}
p->next=s;//±£´æϸö½Úµã
p=p->next;//Çл»ÖÁϸö½Úµã
p->next=NULL;//×îºóÒ»¸ö½ÚµãΪNULL
}
fclose(fp);
return 0;
}
int save(wor *head)
{
FILE *fp;
wor *p;
fp=fopen("c:\\2.txt","ab+");
if(NULL==fp)
{
printf("´ò¿ªÎļþʧ°Ü£¡\n");
return -1;
}
p=head->next;
do{
fwrite(p,sizeof(wor),1,fp);//°ÑÊý¾Ý±£´æµ½fpÖ¸ÏòµÄÎļþÖÐ
p=p->next;
}while(p!=NULL);
fflush(fp);
fclose(fp);
return 0;
}
wor *input(wor *head)
{
wor *s,*p;
if(head==NULL)//Èç¹û±íͷΪ¿Õ£¬ÏÈΪËü·ÖÅäÄÚ´æ
{
head=(wor *)malloc(sizeof(wor));
p=head;
}
else//·ñÔò˵Ã÷ÓÐÄÚÈÝ£¬Ö±½ÓÇл»µ½Î²½Úµã
{
p=head;
while(p->next)
p=p->next;
}
s=(wor *)malloc(sizeof(wor));//ΪÕâ¸ö½Úµã·ÖÅäÄÚ´æ
printf("ÇëÊäÈëѧÉúµÄÐÕÃû£º");
scanf("%s",s->name);
getchar();
printf("ÇëÊäÈëѧÉúµÄѧºÅ£º");
scanf("%ld",&s->num);
p->next=s;//±£´æÕâ¸ö½Úµã
p=p->next;//Çл»ÖÁÏÂÒ»¸ö½Úµã
p->next=NULL;
printf("Òѳɹ¦Ìí¼ÓÐÅÏ¢£¡\n");
return head;
}
int find(wor *head)
{
wor *t;
long n;
int found=0;
wor *p;
if(head==NULL)
{
printf("ûÓмǼѧÉúÐÅÏ¢£¡\n");
return -1;
}
printf("ÇëÊäÈëÄúÒª²éѯµÄѧÉúѧºÅ£º");
scanf("%ld",&n);
p=head;
while(p!=NULL)
{
t=p;
p=p->next;
if(p->num==n)//²éÕÒÓënÏàͬѧºÅµÄѧÉúÐÅÏ¢²¢´òÓ¡µ½ÆÁÄ»
{
found=1;
printf("%s %ld",p->name,p->num);
}
}
if(found!=1)
{
printf("ûÓÐÕÒµ½·ûºÏÒªÇóµÄѧÉúÐÅÏ¢£¡\n");
return -1;
}
return 0;
}
char menu(void)
{
char ch;
printf("  »¶Ó­µÇ½ϵͳ£¡\n");
printf("1  ÊäÈëѧÉúÐÅÏ¢\n");
printf("2  ²éѯѧÉúÐÅÏ¢\n");
printf("0 Í˳öϵͳ\n");
printf("ÇëÊäÈëÄúµÄÑ¡Ôñ£º");
scanf(" %c",&ch);
return ch;
}
int main(void)
{
long n;
char ch;
wor *head=NULL;
while(1)
{
ch=menu();
switch(ch)
{
case '1': 
head=input(head);
save(head);
break;
case '2':
read(&head);
find(head);
break;
case '0':
exit(0);
break;
default :
printf("ÊäÈë´íÎó£¡");
}
}
return 0;

}

加载中
0
Hyacinthus_M
Hyacinthus_M
*head = (wor *)malloc(sizeof(wor));就错了
蔡文豪
蔡文豪
那个函数里head是二级指针啊,难道不是这样用?
返回顶部
顶部