计算登山者 2022-01-13 15:07 采纳率: 100%
浏览 44
已结题

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

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

#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;

}

  • 写回答

1条回答 默认 最新

  • 关注

    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;
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 1月21日
  • 已采纳回答 1月13日
  • 创建了问题 1月13日

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度