程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> c 語言 通訊錄 寫入文本文件(含讀取,刪除某行,修改某行)

c 語言 通訊錄 寫入文本文件(含讀取,刪除某行,修改某行)

編輯:關於C語言

c 語言 通訊錄 寫入文本文件(含讀取,刪除某行,修改某行)


/*   實現一個通訊錄;
通訊錄可以用來存儲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;
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved