[黑马IOS自学第八篇]C语言文件操作,通讯录实现,代码
2015-11-21 09:52
681 查看
一.Static和extern修饰词
<span style="font-size:18px;">#include<stdio.h>
void sum(){
static intcount = 0 ;
printf("%d\n",count);
count++;
}
int main(intargc,const char* argv[]) {
sum();
sum();
sum();
return 0;
}</span>
2).Extern不能用于修饰局部变量
3).Static修饰的函数因为使用的是static函数,修饰的函数是一个内部函数,只能在定义的文件中使用
main.c
#include<stdio.h>
#include"test.h"
externintMax(int,int);//不在test.h声明的时候
int main(int argc,const char * argv[]) {
//int total = sum(223, 21);
int av = avg(10,22);
printf("total = %d\n",av);
int big =Max(98, 44);
printf("big is %d\n",big);
return 0;
}test.c
#include"test.h"
staticint sum(int a ,int b){//warning:unsed function 'sum'
return a+ b ;
}
int avg(int a,int b)
{
returnsum(a,b)/2.0f;//通过内部方法访问static、函数
}
externintMax(int a ,int b)//可以省略extern也可以在其他文件中使用
{
return a>b?a:b;
}test.h
#ifndeftest_h
#definetest_h
#include<stdio.h>
staticint sum(int a,int b);
//externint Max(int a , int b);
int avg(int a,int b);
#endif/* test_h */
2.File文件指针
FILE *fp =NULL;//指向FILE结构指针变量
//文件的打开
fp = fopen("a.txt","r");
//打开成功,返回文件首地址
//打开失败返回NULL
//默认读取文件的路径
///Users/chingwei_macpc/Library/Developer/Xcode/DerivedData/文件概念-heotuhsmlfpzobgrkgssdhyscwzf/Build/Products/Debug/
if (fp != NULL){
printf("打开成功,按任意键退出\n");
}
else{
printf("打开失败,按任意键退出\n");
getchar();
exit(1);//非正常退出
}
//操作文件
//文件关闭函数
//没有关闭函数,可能导致写不进内容至
fclose(fp);
return 0;
2).读写单个字符
int main(intargc,const char* argv[]) {
FILE *fp = NULL;
fp = fopen("a2.txt","w");//w是如果文件名存在,则覆盖然后再写入内容
char ch = 'X',ch2;
if (fp != NULL){
//调用写入字符函数
fputc(ch,fp);
printf("写入字符成功\n");
fclose(fp);
}
fp = fopen("a2.txt","r");
if (fp != NULL){
//调用写入字符函数
ch2 = fgetc(fp);
printf("读取字符%c\n",ch2);
}
fclose(fp);
return 0;
}
3).读取和写入一个字符串
EOF是文件末尾结束标志
文件内部指针每次往后移动位置
int main(intargc,const char* argv[]) {
FILE *fp =NULL;
fp=fopen("te1.txt","w+");//写入并读取,文件不存在再创建
char ch ;
if (fp
4000
!= NULL){
int count = 0 ;
printf("请输入要保存的字符串:");
ch = getchar();
while (ch != '\n') {
count++ ;
fputc(ch, fp);
ch = getchar();
}
printf("本次一共写入%d个字符\n",count);
rewind(fp);//重新指向到文件的首地址
count = 0 ;
ch=fgetc(fp);
while (ch != EOF){
count++;
putchar(ch);
ch = fgetc(fp);
}
printf("本次一共读取%d个字符\n",count);
}
fclose(fp);
return 0;
}
4).字符串读写函数fputs
fgets()安全输入法自动截断超出范围的字符串将最后一个改为'\0'防止越界的问题
gets(str,len,stdin);
puts(str,stdout);
int main(intargc,const char* argv[]) {
FILE *fp = NULL;
fp= fopen("yus.txt","a");//在原有的文件进行追加,不存在空格
char str[]="YUI is BESTCLUBS!\n";
int count = 0 ;
if(fp != NULL)
{
count =fputs(str, fp);//返回值为本次写入的字符长度
printf("本次共写入%d个字符\n",count);
}
fclose(fp);
return 0;
}
会存在空格因为I
str中有存入一个换行字符
当文件使用方式为'a'时候,如果没有文件,则先创建,再写入
#include<stdio.h>
void fputsWrite(){
FILE *fp = NULL;
fp= fopen("yus1.txt","w");//在原有的文件进行追加,不存在空格
char str[]="YUI is BESTCLUBS!\n";
int count = 0 ;
if(fp != NULL)
{
count =fputs(str, fp);//返回值为本次写入的字符长度
printf("本次共写入%d个字符\n",count);
}
fclose(fp);
}
void fgetsRead(){
FILE *fp = NULL;
fp= fopen("yus1.txt","r");
char str2[100];
if(fp != NULL)
{
//从文本读取字符串到字符数组中
// 数组名 长度 fp
//fgets(str2,10, fp);//str2 ----> YUI is BE
fgets(str2, sizeof(str2),fp);
//sizeof(str2)100
//fgets 当文件内容读取完毕时,会给字符加上\0
//当遇到\n或者 EOF读取结束
printf("str2 ---->%s\n",str2);
}
fclose(fp);
}
int main(intargc,const char* argv[]) {
//fputsWrite();
fgetsRead();
return 0;
}
字符串的写入分别保存的,每个字符的ascii码值保存到文件中
#include<stdio.h>
void fputsW(){
FILE *fp = NULL;
fp = fopen("te1.txt","w");
if(fp != NULL)
{
char *s="刘亦菲";//一个汉字占用三个字节
fputs(s, fp);
printf("写入成功\n");
}
fclose(fp);
}
int main(intargc,const char* argv[]) {
FILE *fp = NULL;
fp = fopen("te1.txt","r");
char str2[100];
if(fp != NULL)
{
fgets(str2,4, fp);//读出刘仅仅一个汉字(一个汉字占3字节)
fputs(str2, stdout);
}
fclose(fp);
return 0;
}
4).数据块的读写函数
//
// Created by CHINGWEI_MACPC on 15/11/12.
// Copyright © 2015年 itcast. All rights reserved.
//
#include<stdio.h>
#include<string.h>
void fwriteW(){
FILE *fp = fopen("te1.txt","w");
if (fp != NULL){
char *str ="HelloIOS!";
// 地址 块大小 块数 文件指针
fwrite(str, strlen(str),1, fp);
printf("写入成功%ld!\n",strlen(str));
}
fclose(fp);
}
void fwriteWDB(){
FILE *fp =fopen("D1.data","wb");//二进制
if (fp != NULL){
int a =123 ;
// 地址 块大小 块数 文件指针
fwrite(&a, sizeof(int), 1,fp);
printf("写入成功!\n");
}
fclose(fp);
}
void freadR(){
FILE *fp = fopen("te1.txt","r");
if (fp != NULL){
char str[10] ;//因为存进去的字符数为9
// 地址 块大小 块数 文件指针
fread(str, sizeof(str),1, fp);
printf("读取成功:%s\n",str);
}
fclose(fp);
}
int main(intargc,const char* argv[]) {
//fwriteWDB();
fwriteW();
freadR();
return 0;
}
// main.c
// 从文件中读写结构体信息
//
// Created by CHINGWEI_MACPC on 15/11/12.
// Copyright © 2015年 itcast. All rights reserved.
//
#include<stdio.h>
struct Student{
char name[13];//4*4
int age ;//4*1
float score ;//4*1 //一共24*3 =72生成的文件就占72字节
};
int main(intargc,const char* argv[]) {
struct Studentstu[3]={{"mike",16,95.f},
{"qinqiong",20,87.5},{"uiy",23,98.f}};
FILE *fp = NULL;
fp=fopen("student.data","wb+");
if (fp != NULL){
//写数据到文件
int i = 0 ;
while (i<3) {
fwrite(&stu[i], sizeof(structStudent), 1, fp);
//每次都写入一个元素
i++;
}
printf("写入成功!\n");
rewind(fp);
struct Studentstud2[3];
i = 0 ;
while (i<3) {
fread(&stud2[i], sizeof(structStudent), 1, fp);
printf("学生姓名:%s,年龄:%d,成绩:%.2f\n",stud2[i].name
,stud2[i].age,stud2[i].score);
i++;
}
}
return 0;
}
5).格式化读写函数 fscanf,fprintf
// 格式化读写函数fscanf,fprintf
//
// Created by CHINGWEI_MACPC on 15/11/13.
// Copyright © 2015年 itcast. All rights reserved.
//
#include<stdio.h>
int main(intargc,const char* argv[]) {
FILE *fp = NULL;
fp =fopen("fprintf.txt","w");
if (fp != NULL){
int a = 3 ,b = 4 ,a1 = 0,b1=0;
fprintf(fp, "%d#%d;",a,b);
printf("写入成功!\n");
fclose(fp);
fp =fopen("fprintf.txt","r");
fscanf(fp, "%d#%d;",&a1,&b1);
printf("a1=%d#b1=%d\n",a1,b1);
}
fclose(fp);
return 0;
}
// 读写一个二维数组
//
// Created by CHINGWEI_MACPC on 15/11/13.
// Copyright © 2015年 itcast. All rights reserved.
//
#include<stdio.h>
int main(intargc,const char* argv[]) {
FILE *fp = fopen("Arr.txt","w+");
int a[3][2]={1,2,3,4,5,6};
if (fp != NULL){
for (inti = 0 ; i<3; i++) {
fprintf(fp,"%d,%d\n",a[i][0],a[i][1]);
}
printf("写入成功!\n");
rewind(fp);
int arr2[3][2];
for (inti = 0; i<3; i++) {
fscanf(fp, "%d,%d#",&arr2[i][0],&arr2[i][1]);
}
for(inti = 0 ;i < 3 ;i++){
for (int j =0 ; j<2 ; j++) {
printf("%d\t",arr2[i][j]);
}
printf("-------\n");
}
}
fclose(fp);
return 0;
}
#include<stdio.h>
struct Student{
char name[13];
int age ;
float score ;
};
int main(intargc,const char* argv[]) {
FILE *fp = NULL;
fp= fopen("student.data", "r");
if (fp != NULL){
struct Studentstu ;
//文件定位距离文件首24字节的地方
fseek(fp, sizeof(structStudent),SEEK_SET);
fread(&stu, sizeof(structStudent), 1, fp);
printf("学生姓名:%s,年龄:%d,成绩:%.2f\n",stu.name
,stu.age,stu.score);
}
fclose(fp);
return 0;
}<span style="font-family: 'Microsoft YaHei'; background-color: rgb(255, 255, 255);"> </span>
// main.c
// Feof文件检测函数
//
// Created by CHINGWEI_MACPC on 15/11/13.
// Copyright © 2015年 itcast. All rights reserved.
//
#include<stdio.h>
int main(intargc,const char* argv[]) {
FILE *fp = NULL;
fp = fopen("a2.txt","r");
if (fp != NULL){
char tmp = fgetc(fp);
while(!feof(fp)){
putchar(tmp);
tmp =fgetc(fp);
}
printf("\n");
}
fclose(fp);
return 0;
}
没有出现 \377现象
#include<stdio.h>
int main(intargc,const char* argv[]) {
FILE *fp = NULL;
fp = fopen("arr.txt","r");
if (fp != NULL){
while(!feof(fp)){
char tmp = fgetc(fp);
putchar(tmp);
}
printf("\n");
}
fclose(fp);
return 0;
}
7).文件出错函数
<span style="font-size:18px;">#include<stdio.h>
void sum(){
static intcount = 0 ;
printf("%d\n",count);
count++;
}
int main(intargc,const char* argv[]) {
sum();
sum();
sum();
return 0;
}</span>
2).Extern不能用于修饰局部变量
3).Static修饰的函数因为使用的是static函数,修饰的函数是一个内部函数,只能在定义的文件中使用
main.c
#include<stdio.h>
#include"test.h"
externintMax(int,int);//不在test.h声明的时候
int main(int argc,const char * argv[]) {
//int total = sum(223, 21);
int av = avg(10,22);
printf("total = %d\n",av);
int big =Max(98, 44);
printf("big is %d\n",big);
return 0;
}test.c
#include"test.h"
staticint sum(int a ,int b){//warning:unsed function 'sum'
return a+ b ;
}
int avg(int a,int b)
{
returnsum(a,b)/2.0f;//通过内部方法访问static、函数
}
externintMax(int a ,int b)//可以省略extern也可以在其他文件中使用
{
return a>b?a:b;
}test.h
#ifndeftest_h
#definetest_h
#include<stdio.h>
staticint sum(int a,int b);
//externint Max(int a , int b);
int avg(int a,int b);
#endif/* test_h */
2.File文件指针
FILE *fp =NULL;//指向FILE结构指针变量
//文件的打开
fp = fopen("a.txt","r");
//打开成功,返回文件首地址
//打开失败返回NULL
//默认读取文件的路径
///Users/chingwei_macpc/Library/Developer/Xcode/DerivedData/文件概念-heotuhsmlfpzobgrkgssdhyscwzf/Build/Products/Debug/
if (fp != NULL){
printf("打开成功,按任意键退出\n");
}
else{
printf("打开失败,按任意键退出\n");
getchar();
exit(1);//非正常退出
}
//操作文件
//文件关闭函数
//没有关闭函数,可能导致写不进内容至
fclose(fp);
return 0;
2).读写单个字符
int main(intargc,const char* argv[]) {
FILE *fp = NULL;
fp = fopen("a2.txt","w");//w是如果文件名存在,则覆盖然后再写入内容
char ch = 'X',ch2;
if (fp != NULL){
//调用写入字符函数
fputc(ch,fp);
printf("写入字符成功\n");
fclose(fp);
}
fp = fopen("a2.txt","r");
if (fp != NULL){
//调用写入字符函数
ch2 = fgetc(fp);
printf("读取字符%c\n",ch2);
}
fclose(fp);
return 0;
}
3).读取和写入一个字符串
EOF是文件末尾结束标志
文件内部指针每次往后移动位置
int main(intargc,const char* argv[]) {
FILE *fp =NULL;
fp=fopen("te1.txt","w+");//写入并读取,文件不存在再创建
char ch ;
if (fp
4000
!= NULL){
int count = 0 ;
printf("请输入要保存的字符串:");
ch = getchar();
while (ch != '\n') {
count++ ;
fputc(ch, fp);
ch = getchar();
}
printf("本次一共写入%d个字符\n",count);
rewind(fp);//重新指向到文件的首地址
count = 0 ;
ch=fgetc(fp);
while (ch != EOF){
count++;
putchar(ch);
ch = fgetc(fp);
}
printf("本次一共读取%d个字符\n",count);
}
fclose(fp);
return 0;
}
4).字符串读写函数fputs
fgets()安全输入法自动截断超出范围的字符串将最后一个改为'\0'防止越界的问题
gets(str,len,stdin);
puts(str,stdout);
int main(intargc,const char* argv[]) {
FILE *fp = NULL;
fp= fopen("yus.txt","a");//在原有的文件进行追加,不存在空格
char str[]="YUI is BESTCLUBS!\n";
int count = 0 ;
if(fp != NULL)
{
count =fputs(str, fp);//返回值为本次写入的字符长度
printf("本次共写入%d个字符\n",count);
}
fclose(fp);
return 0;
}
会存在空格因为I
str中有存入一个换行字符
当文件使用方式为'a'时候,如果没有文件,则先创建,再写入
#include<stdio.h>
void fputsWrite(){
FILE *fp = NULL;
fp= fopen("yus1.txt","w");//在原有的文件进行追加,不存在空格
char str[]="YUI is BESTCLUBS!\n";
int count = 0 ;
if(fp != NULL)
{
count =fputs(str, fp);//返回值为本次写入的字符长度
printf("本次共写入%d个字符\n",count);
}
fclose(fp);
}
void fgetsRead(){
FILE *fp = NULL;
fp= fopen("yus1.txt","r");
char str2[100];
if(fp != NULL)
{
//从文本读取字符串到字符数组中
// 数组名 长度 fp
//fgets(str2,10, fp);//str2 ----> YUI is BE
fgets(str2, sizeof(str2),fp);
//sizeof(str2)100
//fgets 当文件内容读取完毕时,会给字符加上\0
//当遇到\n或者 EOF读取结束
printf("str2 ---->%s\n",str2);
}
fclose(fp);
}
int main(intargc,const char* argv[]) {
//fputsWrite();
fgetsRead();
return 0;
}
字符串的写入分别保存的,每个字符的ascii码值保存到文件中
#include<stdio.h>
void fputsW(){
FILE *fp = NULL;
fp = fopen("te1.txt","w");
if(fp != NULL)
{
char *s="刘亦菲";//一个汉字占用三个字节
fputs(s, fp);
printf("写入成功\n");
}
fclose(fp);
}
int main(intargc,const char* argv[]) {
FILE *fp = NULL;
fp = fopen("te1.txt","r");
char str2[100];
if(fp != NULL)
{
fgets(str2,4, fp);//读出刘仅仅一个汉字(一个汉字占3字节)
fputs(str2, stdout);
}
fclose(fp);
return 0;
}
4).数据块的读写函数
//
// Created by CHINGWEI_MACPC on 15/11/12.
// Copyright © 2015年 itcast. All rights reserved.
//
#include<stdio.h>
#include<string.h>
void fwriteW(){
FILE *fp = fopen("te1.txt","w");
if (fp != NULL){
char *str ="HelloIOS!";
// 地址 块大小 块数 文件指针
fwrite(str, strlen(str),1, fp);
printf("写入成功%ld!\n",strlen(str));
}
fclose(fp);
}
void fwriteWDB(){
FILE *fp =fopen("D1.data","wb");//二进制
if (fp != NULL){
int a =123 ;
// 地址 块大小 块数 文件指针
fwrite(&a, sizeof(int), 1,fp);
printf("写入成功!\n");
}
fclose(fp);
}
void freadR(){
FILE *fp = fopen("te1.txt","r");
if (fp != NULL){
char str[10] ;//因为存进去的字符数为9
// 地址 块大小 块数 文件指针
fread(str, sizeof(str),1, fp);
printf("读取成功:%s\n",str);
}
fclose(fp);
}
int main(intargc,const char* argv[]) {
//fwriteWDB();
fwriteW();
freadR();
return 0;
}
// main.c
// 从文件中读写结构体信息
//
// Created by CHINGWEI_MACPC on 15/11/12.
// Copyright © 2015年 itcast. All rights reserved.
//
#include<stdio.h>
struct Student{
char name[13];//4*4
int age ;//4*1
float score ;//4*1 //一共24*3 =72生成的文件就占72字节
};
int main(intargc,const char* argv[]) {
struct Studentstu[3]={{"mike",16,95.f},
{"qinqiong",20,87.5},{"uiy",23,98.f}};
FILE *fp = NULL;
fp=fopen("student.data","wb+");
if (fp != NULL){
//写数据到文件
int i = 0 ;
while (i<3) {
fwrite(&stu[i], sizeof(structStudent), 1, fp);
//每次都写入一个元素
i++;
}
printf("写入成功!\n");
rewind(fp);
struct Studentstud2[3];
i = 0 ;
while (i<3) {
fread(&stud2[i], sizeof(structStudent), 1, fp);
printf("学生姓名:%s,年龄:%d,成绩:%.2f\n",stud2[i].name
,stud2[i].age,stud2[i].score);
i++;
}
}
return 0;
}
5).格式化读写函数 fscanf,fprintf
// 格式化读写函数fscanf,fprintf
//
// Created by CHINGWEI_MACPC on 15/11/13.
// Copyright © 2015年 itcast. All rights reserved.
//
#include<stdio.h>
int main(intargc,const char* argv[]) {
FILE *fp = NULL;
fp =fopen("fprintf.txt","w");
if (fp != NULL){
int a = 3 ,b = 4 ,a1 = 0,b1=0;
fprintf(fp, "%d#%d;",a,b);
printf("写入成功!\n");
fclose(fp);
fp =fopen("fprintf.txt","r");
fscanf(fp, "%d#%d;",&a1,&b1);
printf("a1=%d#b1=%d\n",a1,b1);
}
fclose(fp);
return 0;
}
// 读写一个二维数组
//
// Created by CHINGWEI_MACPC on 15/11/13.
// Copyright © 2015年 itcast. All rights reserved.
//
#include<stdio.h>
int main(intargc,const char* argv[]) {
FILE *fp = fopen("Arr.txt","w+");
int a[3][2]={1,2,3,4,5,6};
if (fp != NULL){
for (inti = 0 ; i<3; i++) {
fprintf(fp,"%d,%d\n",a[i][0],a[i][1]);
}
printf("写入成功!\n");
rewind(fp);
int arr2[3][2];
for (inti = 0; i<3; i++) {
fscanf(fp, "%d,%d#",&arr2[i][0],&arr2[i][1]);
}
for(inti = 0 ;i < 3 ;i++){
for (int j =0 ; j<2 ; j++) {
printf("%d\t",arr2[i][j]);
}
printf("-------\n");
}
}
fclose(fp);
return 0;
}
#include<stdio.h>
struct Student{
char name[13];
int age ;
float score ;
};
int main(intargc,const char* argv[]) {
FILE *fp = NULL;
fp= fopen("student.data", "r");
if (fp != NULL){
struct Studentstu ;
//文件定位距离文件首24字节的地方
fseek(fp, sizeof(structStudent),SEEK_SET);
fread(&stu, sizeof(structStudent), 1, fp);
printf("学生姓名:%s,年龄:%d,成绩:%.2f\n",stu.name
,stu.age,stu.score);
}
fclose(fp);
return 0;
}<span style="font-family: 'Microsoft YaHei'; background-color: rgb(255, 255, 255);"> </span>
// main.c
// Feof文件检测函数
//
// Created by CHINGWEI_MACPC on 15/11/13.
// Copyright © 2015年 itcast. All rights reserved.
//
#include<stdio.h>
int main(intargc,const char* argv[]) {
FILE *fp = NULL;
fp = fopen("a2.txt","r");
if (fp != NULL){
char tmp = fgetc(fp);
while(!feof(fp)){
putchar(tmp);
tmp =fgetc(fp);
}
printf("\n");
}
fclose(fp);
return 0;
}
没有出现 \377现象
#include<stdio.h>
int main(intargc,const char* argv[]) {
FILE *fp = NULL;
fp = fopen("arr.txt","r");
if (fp != NULL){
while(!feof(fp)){
char tmp = fgetc(fp);
putchar(tmp);
}
printf("\n");
}
fclose(fp);
return 0;
}
7).文件出错函数
<span style="font-size:18px;">// // main.c // 15通讯录 // // Created by CHINGWEI_MACPC on 15/11/13. // Copyright © 2015年 itcast. All rights reserved. // #include <stdio.h> #include <string.h> #define N 100 #define NAMELEN 22 #define PHNUMLEN 12 //加上一个\0 //定义联系人结构体 typedef struct { char name[NAMELEN]; char telNum[PHNUMLEN]; }Person; //存储所有联系人数组 Person contacts ;//能够存储100个联系人 //定义联系人的个数 int totalContactCount = 0; //接收用户接收的编号 int fno ; //定义数据文件保存的位置 char *filePath = "telbook.data"; /*******函数声明部分******/ int validateInput(int num , int min ,int max); void doAdd(); void doDelete(); void doUpdate(); void doList(); void doSearchByName(); void doQuit(); void init(); void writeFile(); int main(int argc, const char * argv[]) { init(); printf("通讯录初始化完成\n"); while (1) { printf("\n"); //软件界面显示 printf("********************\n"); printf("****** 欢迎使用通讯录 ******\n"); printf("****** 1、添加联系人 ******\n"); printf("****** 2、删除联系人 ******\n"); printf("****** 3、修改联系人 ******\n"); printf("****** 4、查看所有联系人 ******\n"); printf("****** 5、搜索联系人功能 ******\n"); printf("****** 6、退出系统 ******\n\ b2bd n"); printf("请输入一个编号:"); scanf("%d",&fno); if (validateInput(fno, 1, 6)) { return 0; } switch (fno) { case 1: doAdd(); break; case 2: doDelete(); break; case 3: doUpdate(); break; case 4: doList(); break; case 5: //执行查找 doSearchByName(); break; case 6: //执行退出系统 doQuit(); return 0; break; default: break; } } return 0; } int validateInput(int num , int min ,int max){ if (num < min || num > max) { printf("数不合法"); return 1; } return 0 ; } //文档注释 /** * 添加联系人 */ void doAdd(){ printf("您选择的是添加联系人,请按提示操作\n"); printf("请输入联系人姓名:(*注意联系人姓名中间不能有空格)\n"); scanf("%s",contacts[totalContactCount].name); printf("请输入联系人电话:(*注意联系人电话中间不能有空格)\n"); scanf("%s",contacts[totalContactCount].telNum); int flag ; //确认添加 printf("您确认要添加数据吗?1.确认 0.取消\n"); scanf("%d",&flag); if (flag) { totalContactCount ++ ; writeFile(); }else{ } } //写数据到文件中 void writeFile(){ FILE *fp = fopen(filePath, "wb"); if(fp!= NULL){ fwrite(&totalContactCount , sizeof(totalContactCount), 1, fp); for (int i = 0 ; i< totalContactCount; i++) { fwrite(&contacts[i], sizeof(Person), 1, fp); } printf("数据写入成功!\n"); } } //删除 void doDelete(){ printf("请输入一个要删除的联系人编号,请按提示操作\n"); doList(); int no ; printf("请输入要删除的联系人编号:\n"); scanf("%d",&no); if (validateInput(no, 1, totalContactCount)) { printf("输入不合法\n"); return ; } int flag ; printf("确认是否删除,删除后无法恢复,确认删除吗 1.确认 0.取消\n"); scanf("%d",&flag); if (flag) { if (no == totalContactCount) { totalContactCount--; } else{ for (int i = no ; i< totalContactCount; i++) { contacts[no-1]=contacts[no]; } } totalContactCount --; } writeFile(); } //修改 void doUpdate(){ printf("您选择的是修改联系人,请根据提示操作\n"); doList(); int no ; printf("请输入要修改的联系人编号:\n"); scanf("%d",&no); if (validateInput(no, 1, totalContactCount)) { return ; } char nameTmp[NAMELEN]; char telTmp[12]; printf("输入新的联系人姓名 *注意联系人姓名不能有空格"); scanf("%s",nameTmp); printf("输入新的电话号码 *注意联系人电话号码不能有空格"); scanf("%s",telTmp); int flag ; printf("确认是更新数据,确认更新吗吗 1.确认 0.取消\n"); scanf("%d",&flag); if (flag) { strcpy(contacts[no-1].name, nameTmp); strcpy(contacts[no-1].telNum, telTmp); writeFile(); } printf("更新新的联系人和电话号码成功\n"); } //显示 void doList(){ printf("您选择的是显示所有联系人,联系人数据如下:\n"); //判断通讯录是否为空 if (totalContactCount == 0) { printf("您的通讯录还没有联系人,请添加小伙伴!\n"); } else{ printf("编号\t 姓名\t 电话\t \n"); for (int i = 0 ; i <totalContactCount; i++) { printf("%d\t %s\t %s\t \n",i+1,contacts[i].name,contacts[i].telNum); } } } //通讯录初始化 //1.读取成功,说明文件存在 //2.如果存在要读取联系人数据,保存到联系人数组 contacts //3.如果读取失败,说明我们是第一次使用通讯录 // 1)创建联系人数据文件 // 2)把当前联系人个数写入文件中 void init(){ //定义文件指针 FILE * fp = fopen(filePath, "r"); if(fp != NULL){ //读取联系人个数; 4字节 读完文件指针偏移 fread(&totalContactCount, sizeof(totalContactCount), 1, fp); //读取每一个联系人 for (int i = 0 ; i<totalContactCount; i++) { //读取数据到联系人数组 fread(&contacts[i], sizeof(Person), 1, fp); } } else { //创建文件 fp = fopen(filePath, "wb"); //写入联系人个数 fwrite(&totalContactCount, sizeof(totalContactCount), 1, fp); printf("创建通讯录文件成功\n"); } //关闭文件指针 fclose(fp); } //搜索 void doSearchByName(){ printf("您选择的是搜索联系人,请按提示操作:\n"); doList(); char name[NAMELEN]; printf("请输入你要查找的姓名 *名字之间不能有空格:\n"); scanf("%s",name); int i ; for ( i = 0 ; i< totalContactCount; i++) { if(strcmp(name, contacts[i].name)==0) { printf("您搜索的联系人 %s 的电话号码是 :%s\n",name, contacts[i].telNum); break; } } if (i == totalContactCount) { printf("未找到此人!\n"); } } //退出系统 void doQuit(){ printf("系统正在退出....\n"); printf("系统已经退出!\n"); } </span>
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 如何组织构建多文件 C 语言程序(二)
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 如何写好 C main 函数
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 苹果与Siri的七年之痒:“宫斗”戏码不断上演
- 央视新闻报道XcodeGhost事件
- Lua和C语言的交互详解
- 原生JS仿苹果任务栏菜单,放大效果的菜单
- 关于C语言中参数的传值问题
- 简要对比C语言中三个用于退出进程的函数
- 深入C++中API的问题详解
- 基于C语言string函数的详解
- C语言中fchdir()函数和rewinddir()函数的使用详解
- C语言内存对齐实例详解
- 使用C语言判断英文字符大小写的方法