Lu_Boki
2021-11-14 10:21
采纳率: 100%
浏览 21

C语言文件操作,读写文件不成功


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define NOD struct address_list
struct person
{
    char name[20];
    char address[40];
    long zip;
    char phone[15];
};
NOD
{
    char name[20];
    char address[40];
    long zip;
    char phone[15];
    NOD *next;
};
FILE *fp;
NOD *load(char filename[])
{
    NOD *p,*q,*head;
    struct person per;
    p=head=NULL;
    if((fp=fopen(filename,"r"))==NULL)
        return head;
    else 
        {
            while (!feof(fp))
            {
                if(fread(&per,sizeof(struct person),1,fp)==1)
                    {
                        p=(NOD *)malloc(sizeof(NOD));
                        strcpy(p->name,per.name);
                        strcpy(p->address,per.name);
                        p->zip=per.zip;
                        strcpy(p->phone,per.phone);
                        head=p;
                        p->next=q;
                        q=head;
                    }
            }
        }
    fclose(fp);
    return(head);
}
/*插入一条记录内容*/
NOD *insert(NOD *head)
{
    NOD *temp,*p;
    p=head;
    temp=(NOD *)malloc(sizeof(NOD));
    printf("\n\t请输入姓名:");
    scanf("%s",temp->name);
    printf("\n\t请输入通信地址:");
    scanf("%s",temp->address);
    printf("\n\t请输入6位邮政编码:");
    scanf("%ld",&temp->zip);
    getchar();
    printf("\n\t请输入电话号码:");
    scanf("%s",temp->phone);
    head=temp;
    temp->next=p;
    return head;
}
/*将通信录链表中的内容保存到文件中*/ 
void save(NOD *head,char filename[])
{
    NOD *p;
    struct person per;
    if((fp=fopen(filename,"w"))==NULL)
    {
        printf("文件无法写入");
        exit(0);
    }
    else 
    {
        p=head;
        while(p=NULL)
        {
            strcpy(per.name,p->name);
            strcpy(per.address,p->name);
            per.zip=p->zip;
            strcpy(per.phone,p->phone);
            if(fwrite(&per,sizeof(struct person),1,fp)!=1)
            {
                printf("文件不能写入数据,请检查后重新运行。\n");
                exit(0);
            }
            p=p->next;
        }
        fclose(fp);
    }
}
/*显示通信录全部内容*/
void display(NOD *head)
{
    NOD *p;
    p=head;
    while(p!=NULL)
    {
        printf("name: %s\n",p->name);
        printf("address: %s\n",p->address);
        printf("zip: %ld\n",p->zip);
        printf("phone: %s\n\n",p->phone);
        p=p->next;
    }
}
/*按姓名进行查找*/
void find(NOD *head)
{
    NOD *p;
    char name[20];
    printf("请输入要查找的人的姓名:");
    scanf("%s",name);
    p=head;
    while(p!=NULL)
    {
        if(strcmp(name,p->name)==0)
        {
            printf("name: %s\n",p->name);
            printf("address: %s\n",p->address);
            printf("zip: %ld\n",p->zip);
            printf("phone: %s\n\n",p->phone);
            break;
        }
        else
            p=p->next;
    }
    if(p=NULL)
        printf("\n\t查无此人\n");
}
/*按姓名删除通信录中的一条记录*/
NOD *dele(NOD *head)
{
    NOD *p,*q;
    char name[20];
    printf("请输入要删除记录的姓名:");
    scanf("%s",name);
    p=q=head;
    while(p!=NULL)
    {
        if(strcmp(name,p->name)==0)
        {
            if(head==p)
                head=p->next;
            else
                q->next=p->next;
            free(p);
            break;
        }
        else
        {
            q=p;
            p=p->next;
        }
    }
    if(p==NULL)
        printf("\n\t查无此人\n");
    return head;
} 
/*主函数*/
void main()
{
    NOD *head;
    char fname[20];
    int choise;
    printf("\n\t请输入通信录文件名:");
    scanf("%s",fname);
    head=load(fname);
    while(1)
    {
        printf("\n\t\t通信录管理系统\n");
        printf("\t\t==========================\n");
        printf("\t\t 1. 按姓名查找\n");
        printf("\t\t 2. 按姓名删除\n");
        printf("\t\t 3. 增加新记录\n");
        printf("\t\t 4. 显示所有记录\n");
        printf("\t\t 5. 存盘并退出\n");
        printf("\n\t请选择(1-5):");
        scanf("%d",choise);
        switch(choise)
        {
            case 1: find(head);break;
            case 2: head=dele(head);break;
            case 3: head=insert(head);break;
            case 4: display(head);break;
            case 5: save(head,fname);exit(0);
            default: printf("输入错误,请重新输入!\n");
        }
    }
}

img

img

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • 已采纳

    链表部分写错了啊

    img

    if语句部分修改如下:

    if(fread(&per,sizeof(struct person),1,fp)==1)
    {
        p=(NOD *)malloc(sizeof(NOD));
        strcpy(p->name,per.name);
        strcpy(p->address,per.name);
        p->zip=per.zip;
        strcpy(p->phone,per.phone);
        p->next=NULL;
        if(head==NULL)
        {
            head=p;
            q = head;
        }else
        {
            q->next = p;
            q = p;
        }
        
    }
    
    
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题