Linux下的C语言编程——文件存储链表实现的通讯录

来源:互联网 发布:cs1.5和1.6知乎 编辑:程序博客网 时间:2024/06/06 08:31


研究了好久才把文件加进去,主要是自己太菜了,希望能给大家一点启发!
#include <stdio.h>#include <stdlib.h>#include <string.h>#define N 15struct book// 瀹氫箟缁撴瀯浣?{char name[N];//濮撳悕char sex[N];// 鎬у埆char number[N];//鍙风爜char QQ[N];// qqchar addr[2*N];// 鍦板潃//struct contact *next;};typedef struct book Book;//typedef struct contact *Link;struct contact{Book content;struct contact *next;};typedef struct contact Contact;typedef struct contact *Link;Link creat(){Link p;p = (Link)malloc(sizeof(Contact));if(!(p)){printf("malloc error\n");exit(-1);}p->next = NULL;return p;}void openfile(Link *head){FILE *fp;Book content;Link p1;Link p2;p1 = *head;if((fp = fopen("contact.txt","r+")) == NULL){printf("can not open the file!\n");exit(-1);}else{while(fread(&content,sizeof(content),1,fp) == 1){p2 = creat();p2->content = content;p1->next = p2;p1 = p2;}}}void savefile(Link head){FILE *fp;Link p;Book content;//remove("contact.txt");if((fp = fopen("contact.txt","w+")) == NULL){printf("can not open the file!\n");exit(-1);}p = head->next;while(p != NULL){fwrite(&p->content,sizeof(content),1,fp);p = p->next;}}void creat_list(Link *head)//鍒涘缓閾捐〃{*head = (Link)malloc(sizeof(Contact));if(!(*head)){printf("malloc error!\n");exit(-1);}(*head)->next = NULL;}int len_list(Link *head)//璁$畻閾捐〃闀垮害{Link temp = (*head)->next;int len = 0;int i;while(temp != NULL){temp = temp->next;len++;}return len;}void input(Link p)//杈撳叆鑺傜偣鍊?{printf("\n璇疯緭鍏ヨ仈绯讳汉濮撳悕\n");scanf("%s",p->content.name);printf("璇疯緭鍏ヨ仈绯讳汉鎬у埆\n");scanf("%s",p->content.sex);printf("璇疯緭鍏ヨ仈绯讳汉鐢佃瘽\n");scanf("%s",p->content.number);printf("璇疯緭鍏ヨ仈绯讳汉QQ\n");scanf("%s",p->content.QQ);printf("璇疯緭鍏ヨ仈绯讳汉鍦板潃\n");scanf("%s",p->content.addr);}void output(Link p)//杈撳嚭鑺傜偣鍊?{printf("濮撳悕: %s",p->content.name);printf("\n");printf("鎬у埆: %s",p->content.sex);printf("\n");printf("鐢佃瘽: %s",p->content.number);printf("\n");printf("QQ鍙? %s",p->content.QQ);printf("\n");printf("鍦板潃: %s",p->content.addr);printf("\n");}void insert_tail(Link *head)//灏炬彃娉曞缓绔嬮摼琛?{Link temp = *head;Link p;int n;int i;int m;printf("input the number you want to add!\n");scanf("%d",&n);for(i = 0; i < n; i++){p = (Link)malloc(sizeof(Contact));if(!p){printf("malloc error!\n");exit(-1);}input(p);while(temp->next != NULL){temp = temp->next;}p->next = NULL;temp->next = p;}}void display(Link head)//閬嶅巻閾捐〃{Link temp  = head->next;if(head->next == NULL){printf("contact error!\n");}while(temp != NULL){printf("濮撳悕: %s",temp->content.name);printf("\n");printf("鎬у埆: %s",temp->content.sex);printf("\n");printf("鐢佃瘽: %s",temp->content.number);printf("\n");printf("QQ鍙? %s",temp->content.QQ);printf("\n");printf("鍦板潃: %s",temp->content.addr);printf("\n");temp = temp->next;}}void find_name(Link head)//鎸夊鍚嶆煡鎵?{Link temp = head->next;char str[N];int i = 0;printf("input the name you want to search!\n");scanf("%s",str);if(head->next == NULL){printf("contact empty!\n");}while(temp != NULL){if(strcmp(temp->content.name,str) == 0){output(temp);i++;}temp = temp->next;}if(i == 0){printf("no such persion!\n");}}void find_number(Link head)//鎸夊彿鐮佹煡鎵?{Link temp = head->next;char str[N];int i = 0;printf("input the number you want to search!\n");scanf("%s",str);if(head->next == NULL){printf("contact empty!\n");}while(temp != NULL){if(strcmp(temp->content.number,str) == 0){output(temp);i++;}temp = temp->next;}if(i == 0){printf("no such persion!\n");}}void find_qq(Link head)//鎸塹q鍙锋煡鎵?{Link temp = head->next;char str[N];int i = 0;printf("input the QQ you want to search!\n");scanf("%s",str);if(head->next == NULL){printf("contact empty!\n");}while(temp != NULL){if(strcmp(temp->content.QQ,str) == 0){output(temp);i++;}temp = temp->next;}if(i == 0){printf("no such persion!\n");}}void find_addr(Link head)//鎸夊湴鍧€鏌ユ壘{Link temp = head->next;char str[2*N];int i = 0;printf("input the addr you want to search!\n");scanf("%s",str);if(head->next == NULL){printf("contact empty!\n");}while(temp != NULL){if(strcmp(temp->content.addr,str) == 0){output(temp);i++;}temp = temp->next;}if(i == 0){printf("no such persion!\n");}}void search_node(Link head)//鏌ユ壘鍑芥暟{//Link temp = head->next;int n;char pos;printf("\n1:鎸夊鍚嶆煡鎵綷n2:鎸夌數璇濇煡鎵綷n3:鎸塓Q鍙锋煡鎵綷n4:鎸夊湴鍧€鏌ユ壘\n");printf("璇疯緭鍏ヤ綘鐨勯€夋嫨!\n");scanf("%d",&n);switch(n){case 1:find_name(head);while(1){printf("鏄惁缁х画鏌ユ壘[Y/N]?");scanf("%c",pos);if(pos == 'y' || pos == 'Y'){find_name(head);}else{break;}}break;case 2:find_number(head);while(1){printf("鏄惁缁х画鏌ユ壘[Y/N]?");scanf("%c",pos);if(pos == 'y' || pos == 'Y'){find_number(head);}else{break;}}break;case 3:find_qq(head);while(1){printf("鏄惁缁х画鏌ユ壘[Y/N]?");scanf("%c",pos);if(pos == 'y' || pos == 'Y'){find_qq(head);}else{break;}}break;case 4:find_addr(head);while(1){printf("鏄惁缁х画鏌ユ壘[Y/N]?");scanf("%c",pos);if(pos == 'y' || pos == 'Y'){find_addr(head);}else{break;}}break;default:break;}}void delete_node(Link *head)//鍒犻櫎鍑芥暟{Link temp = (*head)->next;Link p = *head;char str[N];char str1[N];int i = 0;printf("input the name you want to delete!\n");scanf("%s",str);while(temp != NULL){if(strcmp(temp->content.name,str) == 0){output(temp);i++;}temp = temp->next;}temp = (*head)->next;if(i == 0){printf("no such persion!\n");}else if(i == 1){while((strcmp(temp->content.name,str) != 0) && temp->next != NULL){p = temp;temp = temp->next;}if(strcmp(temp->content.name,str) == 0){p->next = temp->next;free(temp);}}else{printf("input the number you want to delete!\n");scanf("%s",str1);while((strcmp(temp->content.number,str1) != 0) && temp->next != NULL){p = temp;temp = temp->next;}if(strcmp(temp->content.number,str1) == 0){p->next = temp->next;free(temp);}}//len_list(head);}void alter_name(Link p)//淇敼濮撳悕{printf("璇疯緭鍏ヤ綘鎯虫敼鎴愮殑鍚嶅瓧!\n");scanf("%s",p->content.name);output(p);}void alter_sex(Link p)// 淇敼鎬у埆{printf("璇疯緭鍏ヤ綘鎯虫敼鎴愮殑鎬у埆!\n");scanf("%s",p->content.sex);output(p);}void alter_number(Link p)//淇敼鍙风爜{printf("璇疯緭鍏ヤ綘鎯虫敼鎴愮殑鍙风爜!\n");scanf("%s",p->content.number);output(p);}void alter_qq(Link p)// 淇敼qq{printf("璇疯緭鍏ヤ綘鎯虫敼鎴愮殑QQ!\n");scanf("%s",p->content.QQ);output(p);}void alter_addr(Link p)// 淇敼鍦板潃{printf("璇疯緭鍏ヤ綘鎯虫敼鎴愮殑鍦板潃!\n");scanf("%s",p->content.addr);output(p);}void atler(Link head)// 淇敼鍑芥暟{int n;char str[2*N];Link temp = head->next;printf("input the name you want to atler!\n");scanf("%s",str);while((strcmp(temp->content.name,str) != 0) && temp->next != NULL){temp = temp->next;}if(strcmp(temp->content.name,str) == 0){output(temp);printf("\n1:淇敼濮撳悕\n2:淇敼鎬у埆\n3:淇敼鍙风爜\n4:淇敼QQ鍙穃n5:淇敼鍦板潃\n");printf("input your choic!\n");scanf("%d",&n);switch(n){case 1:alter_name(temp);break;case 2:alter_sex(temp);break;case 3:alter_number(temp);break;case 4:alter_qq(temp);break;case 5:alter_addr(temp);break;default:printf("input error!\n");break;}}}void clear(Link *head)// 娓呯┖閾捐〃{Link temp = (*head)->next;while(temp != NULL){(*head)->next = (temp->next);free(temp);temp = (*head)->next;}(*head)->next = NULL;}int main(){Link head;int n;char pos[5];creat_list(&head);openfile(&head);system("clear");while(1){printf("\n1:鍒涘缓閫氳褰昞n2閬嶅巻閫氳褰?\n3:鏌ユ壘鑱旂郴浜篭n4:鍒犻櫎鑱旂郴浜篭n5:娣诲姞鑱旂郴浜篭n6:淇敼鑱旂郴浜篭n0:閫€鍑篭n");printf("璇疯緭鍏ヤ綘鐨勯€夋嫨:\n");scanf("%d",&n);switch(n){case 1:insert_tail(&head);break;case 2:display(head);break;case 3:search_node(head);break;case 4:delete_node(&head);while(1){printf("鏄惁缁х画鍒犻櫎[Y/N]?");scanf("%s",pos);if(strcmp(pos,"y")){delete_node(&head);}else{break;}}break;case 5:insert_tail(&head);while(1){printf("鏄惁缁х画娣诲姞[Y/N]?");scanf("%s",pos);if(strcmp(pos,"y") == 0){insert_tail(&head);}else{break;}}break;case 6:atler(head);while(1){printf("鏄惁缁х画淇敼[Y/N]?");scanf("%s",pos);if(strcmp(pos,"y") == 0){atler(head);}else{break;}}break;case 7:clear(&head);break;case 0:savefile(head);exit(0);break;default:printf("input error!\n");break;}}    return 0;}


0 0
原创粉丝点击