将新建链表中的数据录入文件中

大佬们,帮我看看这个程序哪错了,一到要输入数据的时候就不行了

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<string.h>
struct student
{
char num[10];
char name[10];
char clas[10];
char adress[10];
char phonenum[10];
char email[10];
struct student *next;
};

struct student *creat()
{int n;
struct student *head;
struct student *p1,*p2;
n=0;
p1=p2=(struct student *)malloc(sizeof(struct student));
printf("请输入您要录入的学生通讯信息:\n");
printf("\t学号\t姓名\t班级\t地址\t电话\tE-mail\n");
scanf("%s %s %s %s %s %s",p1->num,p1->name,p1->clas,p1->adress,p1->phonenum,p1->email);
head=NULL;
while (strcmp(p1->num,0)!=0);
{
n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(sizeof(struct student));
scanf("%s %s %s %s %s %s",p1->num,p1->name,p1->clas,p1->adress,p1->phonenum,p1->email);
}
p2->next=NULL;
return (head);
}

int main()
{
FILE*fp;
struct student *head;
head=creat();

if((fp=fopen("stu.dat","wb"))==NULL)
{
    printf("打开文件错误,按任意键退出...\n");
    return 0 ;
}
while(1)
{
    if(fwrite(head,sizeof(struct student),1,fp)!=1)
        printf("写文件错误!!\n");
    
}
fclose(fp);
    printf("保存完毕\n");
    return 0;

}

while (strcmp(p1->num,0)!=0);这里错了,后面多了个分号
另外,strcmp是字符串比较,0应该写成“0”
最后的p1没用,释放掉空间

img

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<string.h>
struct student
{
    char num[10];
    char name[10];
    char clas[10];
    char adress[10];
    char phonenum[10];
    char email[10];
    struct student *next;
};

struct student *creat()
{
    int n;
    struct student *head;
    struct student *p1,*p2;
    n=0;
    p1=p2=(struct student *)malloc(sizeof(struct student));
    printf("请输入您要录入的学生通讯信息:\n");
    printf("\t学号\t姓名\t班级\t地址\t电话\tE-mail\n"); 
    scanf("%s %s %s %s %s %s",p1->num,p1->name,p1->clas,p1->adress,p1->phonenum,p1->email);
    head=NULL;
    while (strcmp(p1->num,"0")!=0)
    {
        n=n+1;
        if(n==1)head=p1;
        else p2->next=p1;
        p2=p1;
        p1=(struct student *)malloc(sizeof(struct student));

        scanf("%s %s %s %s %s %s",p1->num,p1->name,p1->clas,p1->adress,p1->phonenum,p1->email);
    }

    p2->next=NULL;

    //删除p1,最后多申请了一个p1,没用到
    free(p1);p1=0;

    return (head);
}

//释放内存
void Release(struct student *head)
{
    struct student *p;
    while(head)
    {
        p = head->next;
        free(head);
        head = p;
    }
}


int main()
{
    FILE*fp;
    struct student *head,*p;
    head=creat();

    if((fp=fopen("stu.dat","wb"))==NULL)
    {
        printf("打开文件错误,按任意键退出...\n");
        return 0 ;
    }
    p = head;
    while(p)
    {
        fprintf(fp,"%s %s %s %s %s %s\n",p->num,p->name,p->clas,p->adress,p->phonenum,p->email);
        p=p->next;

    }
    fclose(fp);
    printf("保存完毕\n");

    Release(head);
    return 0;
}