附带文件操作的通讯录,可以实现链表到文件的写入以及文件到链表的读取

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wwwww_bw/article/details/53576713
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <stdlib.h>
#define MAXSIZE 10
typedef struct NODE
{
    char name[MAXSIZE];
    char p_num[MAXSIZE];
    struct NODE *next;
}node;
typedef struct
{
    char name[MAXSIZE];
    char p_num[MAXSIZE];
}str;
#if 0
void create(node *head);           /*创建联系人 main 194line*/
void del(node *head);              /*删除指定联系人函数*/
void  find(node *head)            /*查找联系人*/
void all_delete(node *head);       /*清空全部联系人*/
void change(node *head);           /*修改联系人信息*/
int display()                      /*菜单*/
#endif
void create(node *head)           /*创建联系人 main 194line*/
{
    node *p;                /*判断联系人是否存在的指针变量*/
   
    int i = 1;
    char name[MAXSIZE];
    char p_num[MAXSIZE];
    printf("*****************请输入姓名:************\n");
    scanf("%s",name);
    //getchar();
    p = head->next;
    while(p != NULL)
    {
        if(strcmp(p->name,name) == 0)
        {
            printf("*********联系人已存在************\n");
            p = NULL;
            i = 0;               /* 联系人已存在,不执行输入手机号命令*/
        }
        else
        {
            p = p->next;
        }
    }
    while( 1 == i)          
    {
        node *q;
        printf("**************请输入手机号:**************\n");
        scanf("%s",p_num);
        q = (node*)malloc(sizeof(node));
       
        if(NULL == q)
        {
            printf("error2\n");
        }
                                                                                    
        strcpy(q->name,name);
        strcpy(q->p_num,p_num);
   
        q->next = head->next;
        head->next = q;         /*touchafa*/
        i = 0;
    }
}
void order(node *head,int len1)  //给联系人排序
{
    node *p;        /*用来排序的变量*/
    node *q;        /*同上*/
    p = head->next;  
    while(len1 > 0)   /*实现联系人按字母排列*/
    {
        while(p != NULL && p->next!= NULL)
        {
            char tempname[MAXSIZE];  /*相当于中间变量用来存储联系人信息*/
            char tempp_num[MAXSIZE];
           
            if(strcmp(p->name,p->next->name) > 0)
            {
                strcpy(tempname, p->next->name);
                strcpy(tempp_num,p->next->p_num);
                strcpy(p->next->name,p->name);
                strcpy(p->next->p_num,p->p_num);
                strcpy(p->name,tempname);
                strcpy(p->p_num,tempp_num);
            }
            else
            {
                p=p->next;
            }
        }
    len1--;       
    }
}
void print(node *head)  /*打印联系人的函数*/
{
    int i = 1; 
    node *p;
    p = head->next;
    printf("现在联系人为:\n");
    printf("编号            姓名          手机号\n");
    while(p != NULL)
    {
        printf("%d               %s           %s\n",i,p->name,p->p_num);
       
        p = p->next;
        i++;
    }
}
void del(node *head)     /*删除指定联系人函数*/
{
    node *p;
    node *q;
   
    q  = head;
    p = q->next;
    char name[MAXSIZE];      
    printf("************请输入要删除的姓名:*********\n");
    scanf("%s",name);
    while((p != NULL) && (strcmp(p->name,name) != 0))
    {
        q = p;
        p = p->next;
    }
    if(NULL == p)
    {
        printf("************未查找到***************\n");
    }
    else
    {
        q->next = p->next;
        free(p);
        printf("***********删除成功****************\n");
    }
}
   
void  find(node *head)   //查找联系人
{
    node *p;
    char name[MAXSIZE];
    printf("*******请输入要查找的姓名:**********\n");
    scanf("%s",name);
    p = head->next;
    while(p != NULL && strcmp(p->name,name) != 0)
    {
        p = p->next;
    }
    if(NULL == p)
    {
        printf("********没有这个联系人*********\n");
    }
    else
    {
        printf("   姓名:%s\n   手机号:%s",p->name,p->p_num);
    }
}
void all_delete(node *head)  //清空全部联系人
{
    while(head->next != NULL)
    {
        node *q;
        q = head->next;
        head->next = q->next;
        free(q);
    }
    if(NULL == head->next)
    {
        printf("*********清除所有联系人成功*********\n");
    }
}
void change(node *head)   //修改联系人信息
{
    node *p;
    char name[MAXSIZE];
    char p_num[MAXSIZE];
    p = head->next;
    printf("请输入要修改的联系人姓名\n");
    scanf("%s",name);
    while( p != NULL)
    {
        if(strcmp(p->name,name) == 0)
        {
            printf("请输入要修改的联系人的手机号\n");
            scanf("%s",p_num);
            strcpy(p->p_num,p_num);
            printf("修改成功\n");
            break;
        }
        else
        {
            p = p->next;
        }
    }
    if(p == NULL)
    {
        printf("未查找到此联系人!\n");
    }
}
int sum(node *head)   //求链表中有多少联系人
{
    int count_node = 0;
    node *p;
    p = head->next;
    if(p != NULL)
    {
        count_node++;
        p = p->next;
    }
    return count_node;
}
 void write_to_file(node *head,int len)  //链表中的联系人信息写到文件
{
    int i;
    str string[100];
    FILE *fp = fopen("a2","wb");
           
    if(NULL == fp)
    {
        printf("open error\n");
        exit(1);
    }
            //printf("1111\n");
    while( head->next != NULL)
    {
        for(i = 0;i < len;i++)
        {
            strcpy(string[i].name,head->next->name);
            strcpy(string[i].p_num,head->next->p_num);
            fwrite(&string[i],sizeof(str),1,fp);   //依次将结构体数组写到文件
            head = head->next;   
        }
    }
    fclose(fp);
}
void read_to_linklist(node *head)    //将文件中的数据读到链表中
{
    int i;
    int m;
    int j = 0;
    node *p;
    node *q;
    p = head;
    FILE *fp;
    fp = fopen("a2","rb");
    fseek(fp,0,SEEK_END);
    i = ftell(fp);
    fseek(fp,0,SEEK_SET);
    m = (i/(sizeof(str)));  // m :文件中有多少个联系人
   
    str string[100];        //结构体数组,存放所有联系人
    fread(string,sizeof(str),m,fp);
   
    while(m > 0)
    {
        q = (node*)malloc(sizeof(node));
        strcpy(q->name,string[j].name);
        strcpy(q->p_num,string[j].p_num);
        m--;
        j++;
        p->next = q;
        p = q;
    }
    fclose(fp);
}
int display()     //菜单
{
printf( "\n**************请选择要进行的操作***********\n");
printf("-----------------------------------------------\n");
printf("*|************* 1 .添加联系人******************|*\n");
printf("*|************* 2 .删除联系人******************|*\n");
printf("*|**************3 .查找联系人******************|*\n");
printf("*|**************4 .显示联系人******************|*\n");
printf("*|**************5 .修改联系人******************|*\n");
printf("*|**************6 .清空联系人******************|*\n");
printf("*|**************7 .退      出******************|*\n");
printf("-----------------------------------------------");
}
int main()
{
    int a;
    int n = 1;
    node *head;
    node *q;

    head = (node*)malloc(sizeof(node));
    if(NULL == head)
    {
        printf("error\n");
    }
    q = head;
    head->next = NULL;
   
    read_to_linklist(q);
   
    while(n == 1)
    {
       
        printf("\n",display());
        printf("\n**********************请输入要进行的操作**********\n\n");
       
        scanf("%d",&a);
        switch(a)
        {
            case 1:
            while(getchar() != '\n');
            create(q);
            order(q,sum(q));
            a = 0;
            break;
           
            case 2:
            while(getchar() != '\n');
            del(q);
            getchar();
            print(q);
            break;
            case 3:
            find(q);
            break;
           
            case 4:
            while(getchar() != '\n');
            print(q);
            break;
          
            case 5:
            while(getchar() != '\n');
            change(q);
            break;
            case 6:
            all_delete(q);
            break;
          
            case 7:
            write_to_file(q,sum(q)); 
            n = 0;
            break;
            default:
            {
                 printf("*****输入错误,请重新选择指令:******\n");
            }
            while(getchar() != '\n');
            break;
        }
    }
    free(head);
}

猜你喜欢

转载自blog.csdn.net/wwwww_bw/article/details/53576713
今日推荐