C语言文件的存储
来源:互联网 发布:java调用函数 编辑:程序博客网 时间:2024/06/08 03:25
综合练习-通讯录管理系统,实现:
1、添加联系人信息
2、按照姓名查找联系人
3、按照姓名删除联系人
4、打印所有联系人信息
5、扩展:使用文件将通讯录信息保存,下次启动时加载数据
使用二进制方式写入文件 C 语言代码如下
#include <stdio.h>#include <string.h>#include <stdlib.h>FILE * g_pAddressListFile;struct PersonInfo { char name[8]; char phoneNumber[13]; char address[20];};void InitFile() { g_pAddressListFile = fopen("AddressList.txt","a+"); if(g_pAddressListFile == NULL) { printf("Can't open the file!\n"); exit(1); }}void ColseFile() { fclose(g_pAddressListFile);}struct PersonInfo MakePersonInfo(char name[8], char phoneNumber[13], char address[20]) { struct PersonInfo info; strcpy(info.name, name); strcpy(info.phoneNumber, phoneNumber); strcpy(info.address, address); return info;}void AddRecord(struct PersonInfo * info) { fseek(g_pAddressListFile, 0, SEEK_END); fwrite(info, sizeof(struct PersonInfo), 1, g_pAddressListFile);}void ClearAllRecord() { g_pAddressListFile = fopen("AddressList.txt","w+"); if(g_pAddressListFile == NULL) { printf("Can't open the file!\n"); exit(1); }}void DeleteInfoByName(char name[8]) { fseek(g_pAddressListFile, 0, SEEK_SET); fseek(g_pAddressListFile, 0, SEEK_END); long fileSize = ftell(g_pAddressListFile); struct PersonInfo * arr = (struct PersonInfo*)malloc(fileSize - 1); struct PersonInfo info; int n=0; fseek(g_pAddressListFile, 0, SEEK_SET); while(fread(&info, sizeof(struct PersonInfo), 1, g_pAddressListFile) != 0) { if(strcmp(info.name, name) != 0) arr[n++] = info; } ClearAllRecord(); for(int i=0; i<n; i++){ AddRecord(&arr[i]); } }struct PersonInfo FindRecordByName(char name[8]) { FILE * pos = g_pAddressListFile; fseek(pos, 0, SEEK_SET); struct PersonInfo info; while (fread(&info, sizeof(struct PersonInfo), 1, pos) != 0) { if(strcmp(info.name, name) == 0) return info; } strcpy(info.name, name); strcpy(info.phoneNumber, ""); strcpy(info.address, ""); return info;}void PrintAddressList() { fseek(g_pAddressListFile, 0, SEEK_SET); struct PersonInfo info; while(fread(&info, sizeof(struct PersonInfo), 1, g_pAddressListFile) != 0) { printf("姓名:%s\n电话号码:%s\n住址:%s\n\n", info.name, info.phoneNumber, info.address); }}// resize rename removeint main(int argc, const char * argv[]) { InitFile(); ClearAllRecord(); struct PersonInfo jone = MakePersonInfo("jone", "119", "heaven"); AddRecord(&jone); struct PersonInfo tom = MakePersonInfo("tom", "120", "hell"); AddRecord(&tom); DeleteInfoByName("tom"); PrintAddressList(); jone = FindRecordByName("jone"); printf("find 姓名:%s\n电话号码:%s\n住址:%s\n\n", jone.name, jone.phoneNumber, jone.address); ColseFile(); return 0;}
0 0
- C语言文件的存储
- 用C语言实现通讯录文件存储的版本
- C 语言文件存储 文本 2进制
- c语言连续存储文件中断
- c语言的存储类
- C语言的存储区
- C语言的存储类别
- C语言--变量的存储
- C语言的存储类型
- C语言的存储类型
- C语言的数据存储
- C语言的存储类别
- C语言的文件
- Linux下的C语言编程——文件存储链表实现的通讯录
- TFT LCD数据存储为BMP文件的C语言代码
- c语言全局变量的忠告 c语言存储类说明符
- c语言全局变量的忠告 c语言存储类说明符
- C语言各个存储区的说明
- 黑马程序员——运算符与语句
- 把妹达人教你一招搞定妹子,说你行你就行
- 泛型
- MySQL主从问题及解决方案
- 如何在windows上安装并使用git.
- C语言文件的存储
- poj1695
- poj2797_106 miles to Chicago(最短路径)
- 数据库分库分表(sharding)系列
- Xtrabackup实现主从
- SGU111 Very simple problem
- 关于Eclipse开发Android无法生成R文件的解决
- 稍大的串—题解
- 第十二周 项目三:日期时间类