電子通訊錄(用檔案寫,掉電可儲存聯絡人)

2021-08-06 01:28:11 字數 4550 閱讀 8587

學習c語言階段寫的乙個程式,實現了乙個類似手機通訊錄的東西,有新增好友,列表資訊,搜尋好友,刪除好友等功能。新增好友或刪除好友時會對配置檔案進行相應操作,程式停止後,再次執行時,先從配置檔案中讀取資訊,搭建鍊錶,上次的資訊便不會丟失。

#ifndef __linklist_h__

#define __linklist_h__

#define true 1

#define false 0

typedef struct

linkdata;

typedef struct _node

node;

void menu(); //選單

node* create_list(); //建立鍊錶

int insert(node *head); //新增好友

int load (node *head); //載入配置檔案

int sort (node *head); //排序

void search(node *head); //搜尋好友

int delete (node *head); //刪除好友

void display(node *head); //顯示好友列表

node* find_element(node* h, char name);

void display_pos(node *h);

int get_len(node * head);

#endif

#include #include #include "linklist.h"

void menu()

int load (node *head)

linkdata buf[10] = ;

int ret;

while (ret = fread (buf, sizeof(linkdata), 1, fp))

if(ret == 0 && !feof(fp))

fclose (fp);

sort (head);

return true;

}void display_label ()

void display (node *head)

}node* create_list()

head->next = null;

return head;

}int sort (node *head)

cur = cur->next;

} tmp = tmp->next;

}return true;

}int refile(int id)

file *fp1 = fopen("bak", "w+");

if (fp1 == null)

linkdata buf[10] = ;

// 拷貝乙個備份

int ret;

while (ret = fread(buf, sizeof(linkdata), 1, fp))

if(ret == 0 && !feof(fp))

fclose(fp);

fclose(fp1);

fp = fopen("peizhi", "w+");

fp1 = fopen("bak", "r+");

//刪除資訊,更新檔案

while (ret = fread(buf, sizeof(linkdata), 1, fp1))

if(ret == 0 && !feof(fp1))

fclose(fp);

fclose(fp1);

return 0;

}int insert_pos(node *head, linkdata buf)

int insert(node *head);

printf ("請輸入好友資訊\n");

printf ("id號 :");

scanf ("%d", &data.id);

printf ("姓名 :");

scanf ("%s", data.name);

printf ("手機號碼 :");

scanf ("%s", data.pnum);

printf ("家庭住址 :");

scanf ("%s", data.addr);

printf ("公司** :");

scanf ("%s", data.cnum);

node->data = data;

node->next = head->next;

head->next = node;

if (node->data.id==0 || node->data.name==null || node->data.pnum==null

|| node->data.addr==null || node->data.cnum==null)

else

fclose (p);

sort (head);

return true;

}node* find_element(node* h, char name)

tmp = tmp->next; }

if (tmp == null)

return false;

return tmp;

}int delete_pos(node* head, int pos)

if (tmp == null)

node *p = tmp->next;

tmp->next = p->next;

free(p);

return true;

}int count_num(node *head, char *name)

tmp = tmp->next; }

return count;

}void search(node *head)

}int delete (node *head)

tmp = tmp->next;

} if (tmp == null)

return false;

node *p = tmp->next;

tmp->next = p->next;

refile (p->data.id);

free(p);

printf ("刪除成功\n");

return true;

} else

printf ("請輸入id號指定刪除好友:");

int id;

scanf ("%d",&id);

node *tmp = head;

while (tmp)

tmp = tmp->next;

} if (tmp == null)

return false;

node *p = tmp->next;

tmp->next = p->next;

free(p);

printf ("刪除成功\n");

refile (id);

return true; }}

void display_pos(node *h)

int get_len(node * head)

return len;

}

#include #include "linklist.h"

#include int main()

; linkdata f2 = ;

linkdata f3 = ;

linkdata f4 = ;

linkdata f5 = ;

linkdata buf[10];

buf[0] = f1;

buf[1] = f2;

buf[2] = f3;

buf[3] = f4;

buf[4] = f5;

//往配置檔案內寫入內建聯絡人,方便測試功能,最好寫入後將此部分注釋,避免重複寫入

file *fd = fopen ("peizhi", "w+");

fwrite (buf, sizeof(linkdata), 1, fd);

fwrite (buf+1, sizeof(linkdata), 1, fd);

fwrite (buf+2, sizeof(linkdata), 1, fd);

fwrite (buf+3, sizeof(linkdata), 1, fd);

fwrite (buf+4, sizeof(linkdata), 1, fd);

fclose (fd);

load (head); //載入配置檔案

char str[20];

while(1)

printf ("請按任意鍵返回選單:");

getchar();

getchar();

printf ("\n");

continue;

}return 0;

}

通訊錄 檔案儲存

本文是基於動態儲存的通訊錄實現中加以修改 動態儲存實現通訊錄 增加資料寫入檔案函式 將資料寫入檔案 void writefile mindan book for int i 0 i book size i fclose fp 增加載入檔案資料儲存 void loadfile mindan book ...

使用檔案實現通訊錄的儲存

在使用動態儲存的時候發現每次程式結束儲存的資訊都會消失,但儲存在檔案中就輕鬆的解決了這個問題。實現 include include include define ok 1 define error 0 typedef int elementtype typedef int status typede...

利用檔案知識寫乙個通訊錄

注意檔案在主函式中插入的位置 include include include friendadress.h int main menu while 1 return 0 include friendadress.h include include includevoid sort node h el...