c语言文件操作

本文介绍了c语言的文件操作,重点阐释了各个文件操作函数的作用,总结了日常使用文件操作的方法,加深对文件操作的理解。

一、文件

1.文件分类

文件可以分为为文本文件或者二进制文件。数据在内存中以二进制的形式存储,如果不加转换的输出到外存,就是二进制文件。如果要求在外存上以ASCII码的形式存储,则需要在存储前转换。以ASCII字符的形式存储的文件就是文本文件。

2.文件缓冲区

ANSIC 标准采用“缓冲文件系统”处理的数据文件的,所谓缓冲文件系统是指系统自动地在内存中为程序中每一个正在使用的文件开辟一块“文件缓冲区”。从内存向磁盘输出数据会先送到内存中的缓冲区,装满缓冲区后才一起送到磁盘上。如果从磁盘向计算机读入数据,则从磁盘文件中读取数据输入到内存缓冲区(充满缓冲区),然后再从缓 冲区逐个地将数据送到程序数据区(程序变量等)。缓冲区的大小根据C编译系统决定。
简单说,就是把要处理的都赞到一起,等攒够了在送给系统统一处理。(有点像垃圾桶,等垃圾桶装满了在去倒)。
与缓存的区别
于此对应的还有一个缓存,这两个是不同的概念,缓存是经常使用的数据开辟一块空间存进去,而这块空间就是缓存。虽然两个都是为提升程序的运行效率,但是还是有本质的区别的。

二、文件操作函数介绍

1.打开关闭文件函数

1FILE * fopen ( const char * filename, const char * mode ); 2int fclose ( FILE * stream ); 3 4

打开方式如下(网上找的比较全的图,t可以省略)
在这里插入图片描述
简单打开文件的例子:

1#include<stdio.h> 2int main () 3{ 4 FILE * pFile; 5 pFile = fopen ("test.txt","w"); //写方式打开 6 if (pFile!=NULL) 7 { 8 fputs ("fopen example",pFile); 9 fclose (pFile); 10 } 11 return 0; 12 } 13 14

2.读写操作

全部读写函数如下:在这里插入图片描述
着重介绍以下四个常用的读写操作:
2.1 fread()

1size_t fread( void *buffer, size_t size, size_t count, FILE *stream ); 2 3
1#include<stdio.h> 2int main() 3{ 4 FILE *pFile; 5 char buff[1024] = "hello"; 6 pFile = fopen("test.txt", "w+"); 7 fwrite(buff, 1,11,pFile); 8 fclose(pFile); 9 return 0; 10} 11 12
1#include<stdio.h> 2int main() 3{ 4 FILE *pFile; 5 char buff[1024] = " "; 6 pFile = fopen("test.txt", "w+"); 7 fread(buff, 1,11,pFile); 8 fclose(pFile); 9 return 0; 10} 11 12

2.2 fwrite()

1size_t fwrite( const void *buffer, size_t size, size_t count, FILE *stream ); 2 3
1#include<stdio.h> 2int main() 3{ 4 FILE *pFile; 5 char buff[1024] = "hello"; 6 pFile = fopen("test.txt", "w+"); 7 fwrite(buff, 1,11,pFile); 8 fclose(pFile); 9 return 0; 10} 11 12

2.3 fprintf()

1int fprintf( FILE *stream, const char *format [, argument ]...); 2 3
1#include<stdio.h> 2int main() 3{ 4 FILE *pFile; 5 pFile = fopen("test.txt", "w+"); 6 fprintf(pFile, "hello world"); 7 fclose(pFile); 8 return 0; 9} 10 11

2.4 fscanf()

1int fscanf( FILE *stream, const char *format [, argument ]... ); 2 3

格式化读入,文件格式如图:
在这里插入图片描述

1#include<stdio.h> 2int main() 3{ 4 FILE *pFile; 5 char buff[1024] = ""; 6 int num = 5; 7 char str = ""; 8 pFile = fopen("test.txt", "r+"); 9 fscanf(pFile, "%s %d %s", buff, &num, &str); 10 fclose(pFile); 11 return 0; 12} 13 14

在这里插入图片描述

三、通讯录最终文件版

1#define _CRT_SECURE_NO_WARNINGS 2#include <stdio.h> 3#include <stdlib.h> 4#include <string.h> 5typedef void(*Func)(); 6typedef struct PersonInfo 7{ 8 char name[1024]; 9 char sex[1024]; 10 char age[1024]; 11 char phone[1024]; 12 char adress[1024]; 13} PersonInfo; 14typedef struct AdressBook 15{ 16 17 PersonInfo *persons; 18 int capacity; 19 int size; 20 21} AdressBook; 22 23AdressBook g_address_book; 24void Init() 25{ 26 g_address_book.size = 0; 27 g_address_book.capacity = 100; 28 g_address_book.persons = (PersonInfo *)malloc(g_address_book.capacity * sizeof(PersonInfo)); 29 for (int i = 0; i < g_address_book.capacity; i++) 30 { 31 g_address_book.persons[i].name[0] = '\0'; 32 g_address_book.persons[i].phone[0] = '\0'; 33 g_address_book.persons[i].adress[0] = '\0'; 34 g_address_book.persons[i].sex[0] = '\0'; 35 g_address_book.persons[i].age[0] = '\0'; 36 } 37} 38int Menu() 39{ 40 int i = -1; 41 printf("========================\n"); 42 printf(" 1. 添加联系人信息\n"); 43 printf(" 2. 删除指定联系人信息\n"); 44 printf(" 3. 查找指定联系人信息\n"); 45 printf(" 4. 修改指定联系人信息\n"); 46 printf(" 5. 显示所有联系人信息\n"); 47 printf(" 6. 清空所有联系人\n"); 48 printf(" 7. 排序(以名字排序)\n"); 49 printf(" 8 .保存联系人到文件\n"); 50 printf(" 9. 加载联系人\n"); 51 printf(" 0. 退出\n"); 52 printf("========================\n"); 53 printf("请输入要选择操作的序号:"); 54 fflush(stdin); 55 scanf("%d", &i); 56 return i; 57} 58 59int Judge_Input_Legal(int choice) 60{ 61 if (choice < 0 || choice > 9) 62 { 63 printf("输入有误请重新输入!\n"); 64 return -1; 65 } 66 if (choice == 0) 67 { 68 printf("Goodbye\n"); 69 return 0; 70 } 71 return choice; 72} 73 74void Empty() 75{ 76 //TMOD 77} 78void Add_Contact() 79{ 80 PersonInfo *person_info = &g_address_book.persons[g_address_book.size]; 81 if (g_address_book.size >= g_address_book.capacity) 82 { 83 printf("内存已满进行扩容"); 84 g_address_book.capacity = 2 * g_address_book.capacity; 85 //g_address_book.persons = (PersonInfo *)realloc(g_address_book.persons, g_address_book.capacity * sizeof(PersonInfo)); 86 PersonInfo *p = (PersonInfo *)malloc(g_address_book.capacity * sizeof(PersonInfo)); 87 for (int i = 0; i < g_address_book.size; i++) 88 { 89 p[i] = person_info[i]; 90 } 91 free(g_address_book.persons); 92 g_address_book.persons = p; 93 } 94 printf("请输入要添加联系人的信息:\n"); 95 printf("输入姓名:"); 96 scanf("%s", person_info->name); 97 printf("输入性别:"); 98 scanf("%s", person_info->sex); 99 printf("输入年龄:"); 100 scanf("%s", person_info->age); 101 printf("输入电话:"); 102 scanf("%s", person_info->phone); 103 printf("输入地址:"); 104 scanf("%s", person_info->adress); 105 g_address_book.size++; 106 printf("添加联系人成功\n"); 107} 108 109void Delet_Contact() 110{ 111 PersonInfo *person_info = &g_address_book.persons; 112 int num = 0; 113 printf("请输入要删除联系人的序号:"); 114 scanf("%d", &num); 115 if (num < 0 || num >=g_address_book.size) 116 { 117 printf("输入有误"); 118 return; 119 } 120 *(person_info + num) = *(person_info + g_address_book.size - 1); 121 g_address_book.size--; 122 printf("删除联系人成功\n"); 123} 124 125void Search_Contact() 126{ 127 char search_name[1024] = " "; 128 PersonInfo *person_info = &g_address_book.persons; 129 printf("请输入要查找的姓名:"); 130 scanf("%s", search_name); 131 for (int i = 0; i < g_address_book.size; i++) 132 { 133 if (0 == strcmp((g_address_book.persons + i)->name, search_name)) 134 { 135 printf("[%d]\t%s\t%s\t%s\t%s\t%s\n", 136 i, 137 (person_info + i)->name, 138 (person_info + i)->sex, 139 (person_info + i)->age, 140 (person_info + i)->phone, 141 (person_info + i)->adress 142 ); 143 } 144 } 145 146} 147 148void Update_Contact() 149{ 150 int num = 0; 151 PersonInfo *person_info = &g_address_book.persons; 152 printf("请输入要修改的序号:"); 153 scanf("%d", &num); 154 printf("请输入新的姓名:"); 155 scanf("%s", (person_info + num)->name); 156 printf("请输入新的性别:"); 157 scanf("%s", (person_info + num)->sex); 158 printf("请输入新的年龄:"); 159 scanf("%s", (person_info + num)->age); 160 printf("请输入新的电话:"); 161 scanf("%s", (person_info + num)->phone); 162 printf("请输入新的住址:"); 163 scanf("%s", (person_info + num)->adress); 164 printf("联系人信息修改成功\n"); 165} 166 167void Print_Contact() 168{ 169 PersonInfo *person_info = &g_address_book.persons; 170 printf("打印共%d条联系人信息如下:\n", g_address_book.size); 171 printf("序号\t姓名\t性别\t年龄\t电话\t住址\n"); 172 for (int i = 0; i < g_address_book.size; i++) 173 { 174 printf("[%d]\t%s\t%s\t%s\t%s\t%s\n", 175 i, 176 (g_address_book.persons + i)->name, 177 (g_address_book.persons+ i)->sex, 178 (g_address_book.persons+ i)->age, 179 (g_address_book.persons+ i)->phone, 180 (g_address_book.persons+ i)->adress 181 ); 182 } 183 printf("打印全部信息成功\n"); 184} 185 186void Clear_Contact() 187{ 188 g_address_book.size = 0; 189 printf("全部清空成功"); 190} 191 192void Sort_Contact() 193{ 194 PersonInfo *person_info = &g_address_book.persons; 195 //PersonInfo *PersonInfo_tmp = &g_address_book.persons[g_address_book.size+1]; 196 char PersonInfo_tmp[1024] = " "; 197 for (int i = 0; i < g_address_book.size; i++) 198 { 199 for (int j = 0; j < g_address_book.size - i-1; j++) 200 { 201 if (strcmp((person_info + j)->name, (person_info + j + 1)->name) > 0) 202 { 203 //*PersonInfo_tmp = *(person_info + j); 204 //*(person_info + j) = *(person_info + j + 1); 205 //*(person_info + j + 1) = *PersonInfo_tmp; 206 strcpy(PersonInfo_tmp, person_info + j); 207 strcpy(person_info + j, person_info + j + 1); 208 strcpy(person_info + j + 1, PersonInfo_tmp); 209 } 210 } 211 } 212 printf("排序成功\n"); 213} 214 215void Save_Contact_file() 216{ 217 PersonInfo *person_info = &g_address_book.persons; 218 FILE *pFile; 219 pFile = fopen("Contacts.txt", "w"); 220 fprintf(pFile, "%d\n",g_address_book.size); 221 fprintf(pFile, "序号\t姓名\t性别\t年龄\t电话\t住址\n"); 222 for (int i = 0; i < g_address_book.size; i++) 223 { 224 fprintf(pFile,"[%d]\t%s\t%s\t%s\t%s\t%s\n", 225 i, 226 (g_address_book.persons + i)->name, 227 (g_address_book.persons+ i)->sex, 228 (g_address_book.persons+ i)->age, 229 (g_address_book.persons+ i)->phone, 230 (g_address_book.persons+ i)->adress 231 ); 232 } 233 fclose(pFile); 234 printf("保存信息成功!\n"); 235} 236 237void Load_Contact() 238{ 239 int num = 0; 240 PersonInfo *person_info = &g_address_book.persons; 241 FILE *pFile; 242 pFile = fopen("Contacts.txt", "r"); 243 fscanf(pFile, "%d\n",&g_address_book.size); 244 fscanf(pFile, "%[^\n]%*c\n"); 245 for (int i = 0; i < g_address_book.size; i++) 246 { 247 fscanf(pFile, "[%d]\t%s\t%s\t%s\t%s\t%s\n", 248 &num, 249 (g_address_book.persons + i)->name, 250 (g_address_book.persons+ i)->sex, 251 (g_address_book.persons+ i)->age, 252 (g_address_book.persons+ i)->phone, 253 (g_address_book.persons+ i)->adress 254 ); 255 } 256 fclose(pFile); 257 printf("信息读取成功!\n"); 258 259} 260int main() 261{ 262 Func arr[] = 263 { 264 Empty,//无作用,只为把数组0号元素占用,使序号与数组下标对应 265 Add_Contact, 266 Delet_Contact, 267 Search_Contact, 268 Update_Contact, 269 Print_Contact, 270 Clear_Contact, 271 Sort_Contact, 272 Save_Contact_file, 273 Load_Contact 274 }; 275 Init(); 276 while (1) 277 { 278 int choice = Menu(); //打印菜单并读取用户输入 279 choice = Judge_Input_Legal(choice); 280 if(choice == 0) break; //判断用户输入是否合法 281 if(choice < 0) continue; 282 arr[choice]();//转移表调用函数 283 } 284 return 0; 285} 286 287
上一篇:Python文件操作
下一篇:Linux文件操作

代码交流 2021