欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c 语言 通讯录 写入文本文件(含读取,删除某行,修改某行)

程序员文章站 2022-03-25 07:52:01
/* 实现一个通讯录; 通讯录可以用来存储1000个人的信息,每个人的信息包括: 姓名、性别、年龄、电话、住址 提供方法: 1. 添加联系人信息 2....
/*   实现一个通讯录;
通讯录可以用来存储1000个人的信息,每个人的信息包括:
姓名、性别、年龄、电话、住址
  
提供方法:
1.  添加联系人信息
2.  删除指定联系人信息
3.  查找指定联系人信息
4.  修改指定联系人信息
5.  显示所有联系人信息
6.  清空所有联系人 
7.  显示文本电话簿信息  */
//头文件
#ifndef __PHONE_BOOK_H__
#define __PHONE_BOOK_H__
typedef struct phone_book
{
    char name[10];
    char sex[5];
    char addr[20];
    int year;
    char phone_num[12];
    struct phone_book *pNext;
}Book,*pBook;
pBook add_linkman(pBook head);
pBook remove_linkman(pBook head);
void traver_linkman(pBook head);
pBook empty_linkman(pBook head);
pBook amend_linkman(pBook head);
void search_linkman(pBook head);
void start(pBook *head);
void write_file(pBook position);//写入文本
void read_file(pBook head);//读取文本信息
void amend_file(pBook position, char *name, int flag);//修改,删除文本中的某一行信息
#endif
//函数文件
#include<stdio.h>
#include"phone_book.h"
#include<string.h>
#include<assert.h>
extern char file_name[30]="\0";
void start(pBook *phead)
{
    int i = -1;
    assert(phead);
    while (1)
    {
        printf("欢迎使用电话薄系统!选择您的操作:\n\n");
        printf("*********************************************\n"
               "*1.添加联系人信息     2.删除指定联系人信息  *\n"
               "*3.查找指定联系人信息 4.修改指定联系人信息  *\n"
               "*5.显示所有联系人信息 6.清空所有联系人      *\n"
               "*7.显示电话文本内信息 0.结束系统!          *\n"
               "*        ————中情局专用————         *\n"
               "*********************************************\n");
        scanf("%d",&i);
        switch (i)
        {
        case 0:
            printf("*****感谢使用本系统!*****\n");
            printf("  您的电话本存储路径为:%s  别忘记哦!\n", file_name);
            exit(0);
        case 1:
            *phead=add_linkman( phead);
            break;
        case 2:
            *phead = remove_linkman(phead);
            break;
        case 3:
             search_linkman(phead);
            break;
        case 4:
            *phead = amend_linkman(phead);
            break;
        case 5:
            traver_linkman(phead);
            break;
        case 6:
            empty_linkman( phead);
            break;
        case 7:
            read_file(phead);
            break;
        default:
            printf("选择有误!请重新选择\n");
            break;
        }      
    }
}
pBook add_linkman(pBook head)
{
    int i = 0;
    char name[10];
    char sex[5];
    char addr[20];
    int year;
    long phone_num;
    pBook pBase = head;
     
    assert(head);
     
flag:
    printf("你要添加多少个联系人:");
    scanf("%d",&i);
    if (i <= 0)
    {
        printf("输入数量错误!\n");
        goto flag;
    }
    if (head->pNext == NULL)
    {
        while (i--)
        {
            pBook pNew = (pBook)malloc(sizeof(Book));
            assert(pNew);
            printf("请输入联系人信息:(姓名、性别、地址、年龄、电话号码)\n");
            scanf("%s", name);
            scanf("%s", sex);
            scanf("%s", addr);
            strcpy(pNew->name, name);
            strcpy(pNew->sex, sex);
            strcpy(pNew->addr, addr);
            scanf("%d", &pNew->year);
            scanf("%s", &pNew->phone_num);
            pBase->pNext = pNew;
            pNew->pNext = NULL;
            pBase = pNew;
            write_file(pNew);
        }
    }
    else
    {
        while (pBase->pNext != NULL)
        {
            pBase = pBase->pNext;
        }
        while (i--)
        {
            pBook pNew = (pBook)malloc(sizeof(Book));
            assert(pNew);
            printf("请输入联系人信息:(姓名、性别、地址、年龄、电话号码)\n");
            scanf("%s", name);
            scanf("%s", sex);
            scanf("%s", addr);
            strcpy(pNew->name, name);
            strcpy(pNew->sex, sex);
            strcpy(pNew->addr, addr);
            scanf("%d", &pNew->year);
            scanf("%s", &pNew->phone_num);
            pBase->pNext = pNew;
            pNew->pNext = NULL;
            pBase = pNew;
            write_file(pNew);
        }
    }
    return head;
}
pBook remove_linkman(pBook head)
{
    pBook link_man = head;
    char name[10];
    if (!head)
    {
        printf("没有联系人,不能删除!\n");
        return NULL;
    }
    printf("请问你要删除哪位联系人?  ");
    scanf("%s",name);
    while (link_man->pNext!=NULL)
    {
        if (strcmp(link_man->pNext->name, name) == 0)
        {
            amend_file(link_man->pNext,name,0);
            if (link_man->pNext->pNext == NULL)
            {
                free(link_man->pNext);
                link_man->pNext = NULL;
                printf("删除完成!\n");
                 
            }
            else
            {
                pBook ptmp = NULL;
                ptmp = link_man->pNext;
                link_man->pNext = link_man->pNext->pNext;
                free(ptmp);
                printf("删除完成!\n");
                 
            }
             
            return head;
        }
        else
        {
            link_man = link_man->pNext;
        }
    }
    printf("没有找到该联系人!\n");
    return head;
}
void traver_linkman(pBook head)
{
    pBook link_man = head;
    if (head->pNext == NULL)
    {
        printf("电话簿为空!\n");
    }
    while (link_man->pNext!=NULL)
    {
        printf("%s\n", link_man->pNext->name);
        printf("%s\n", link_man->pNext->sex);
        printf("%s\n", link_man->pNext->addr);
        printf("%d\n", link_man->pNext->year);
        printf("%s\n", link_man->pNext->phone_num);
        printf("***********************************\n");
        link_man = link_man->pNext;
    }
}
pBook empty_linkman(pBook head)
{
    pBook ptmp = NULL;
    pBook link_man = head;
    if (head->pNext == NULL)
    {
        printf("电话簿为空!不用清空哦!(ˇ0ˇ) \n");
        return NULL;
    }
    while (link_man->pNext != NULL)
    {
        if (link_man->pNext->pNext == NULL)
        {
            free(link_man->pNext);
            link_man->pNext = NULL;
        }
        else
        {
            ptmp = link_man->pNext;
            link_man->pNext = link_man->pNext->pNext;
            free(ptmp);
             
        }
    }
     
    printf("清空完成!\n");
    write_file(NULL);
    return NULL;
}
pBook amend_linkman(pBook head)
{
    char name[10];
    char sex[5];
    char addr[20];
    pBook link_man = head;
    if (!head)
    {
        printf("没有联系人,不能修改!\n");
        return NULL;
    }
    printf("请问你要修改哪位联系人?  ");
    scanf("%s", name);
    while (link_man->pNext != NULL)
    {
        if (strcmp(link_man->pNext->name, name) == 0)
        {
            printf("请输入联系人信息:(姓名、性别、地址、年龄、电话号码)\n");
            scanf("%s", name);
            scanf("%s", sex);
            scanf("%s", addr);
            strcpy(link_man->pNext->name, name);
            strcpy(link_man->pNext->sex, sex);
            strcpy(link_man->pNext->addr, addr);
            scanf("%d", &link_man->pNext->year);
            scanf("%s", &link_man->pNext->phone_num);
            amend_file(link_man->pNext,name,1);
            return head;
        }
        else
            link_man = link_man->pNext;
    }
    return head;
}
void search_linkman(pBook head)
{
    char name[10];
    pBook link_man = head;
     
    if (!head)
    {
        printf("没有联系人,查找失败!\n");
        return NULL;
    }
    printf("请问你要查找哪位联系人?  ");
    scanf("%s", name);
    while (link_man->pNext != NULL)
    {
        if (strcmp(link_man->pNext->name, name) == 0)
        {
            printf("%s\n", link_man->pNext->name);
            printf("%s\n", link_man->pNext->sex);
            printf("%s\n", link_man->pNext->addr);
            printf("%d\n", link_man->pNext->year);
            printf("%s\n", link_man->pNext->phone_num);
            printf("***********************************\n");
            return 0;
        }
        else
            link_man = link_man->pNext;
    }
    printf("没有找到!\n");
}
void write_file(pBook position)
{  
    FILE *list;
    if (position != NULL)
    {  
        if (strcmp(file_name,"\0")==0)
        {
            printf("请输入你要存储的文件路径:");
            scanf("%s", file_name);
            list = fopen(file_name, "wt");
            fprintf(list, "%-10s", "姓名");
            fprintf(list, "%-6s", "性别");
            fprintf(list, "%-10s", "地址");
            fprintf(list, "%-8s", "年龄");
            fprintf(list, "%-20s", "号码");
            fprintf(list, "%s", "\n");
        }
        else
            list = fopen(file_name, "a+");
        fprintf(list,"%-10s",position->name);   
        fprintf(list, "%-6s", position->sex);
        fprintf(list, "%-10s", position->addr);
        fprintf(list, "%-8d", position->year);
        fprintf(list, "%-20s", position->phone_num);
        fprintf(list, "%s", "\n");
    }
    else
    {
        list = fopen(file_name, "w");
        fprintf(list, "%s", "   已经销毁的通讯录  ");
        strcpy(file_name,"\0");
    }
        fclose(list);
}
void read_file(pBook head)
{
    if (head->pNext == NULL)
    {
        printf("空的通讯录\n\n");
    }
    else
    {
        FILE *list = fopen(file_name, "r");
        char ch;
        while ((ch = fgetc(list)) != EOF)
        {
            putchar(ch);
        }
    }
}
void amend_file(pBook position,char *name,int i)
{
    char str[100];
    long num1=0;
    FILE * list = fopen(file_name, "r");
    while (fgets(str, 100, list)!=NULL)
    {      
        if (strstr(str, name) == NULL)
        {
            num1 += strlen(str) + 1;
        }
        else
            break;
    }
    fclose(list);
    list = fopen(file_name, "r+");
    fseek(list, num1, SEEK_SET);
    if (i == 0)
    {
        fprintf(list, "%54s"," ");
        fprintf(list, "%s", "\n");
    }
    else
    {
        fprintf(list, "%-10s", position->name);
        fprintf(list, "%-6s", position->sex);
        fprintf(list, "%-10s", position->addr);
        fprintf(list, "%-8d", position->year);
        fprintf(list, "%-20s", position->phone_num);
        fprintf(list, "%s", "\n");
    }
    fclose(list);
}
//主函数
#include<stdio.h>
#include"phone_book.h"
char file_name[30];
int main()
{
     pBook phead = NULL;
    start(&phead);
    return 0;
}