1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
| #include <stdio.h> #include <stdlib.h> #include <string.h>
void create_text_file(); void read_text_file(); void binary_file_operations(); void file_positioning(); void error_handling();
struct Person { char name[50]; int age; float height; };
int main() { printf("=== 创建和写入文本文件 ===\n"); create_text_file(); printf("\n"); printf("=== 读取文本文件 ===\n"); read_text_file(); printf("\n"); printf("=== 二进制文件操作 ===\n"); binary_file_operations(); printf("\n"); printf("=== 文件定位 ===\n"); file_positioning(); printf("\n"); printf("=== 错误处理 ===\n"); error_handling(); return 0; }
void create_text_file() { FILE *file; file = fopen("test.txt", "w"); if (file == NULL) { printf("无法打开文件!\n"); return; } fprintf(file, "姓名:张三\n"); fprintf(file, "年龄:%d\n", 25); fprintf(file, "成绩:%.2f\n", 95.5); fputs("这是使用fputs写入的一行文本。\n", file); fputs("这是第二行。\n", file); fputc('A', file); fputc('B', file); fputc('C', file); fputc('\n', file); fclose(file); printf("文本文件创建并写入成功!\n"); }
void read_text_file() { FILE *file; char buffer[256]; char ch; file = fopen("test.txt", "r"); if (file == NULL) { printf("无法打开文件!\n"); return; } printf("=== 使用fgets读取文件内容 ===\n"); while (fgets(buffer, sizeof(buffer), file) != NULL) { printf("%s", buffer); } rewind(file); printf("\n=== 使用fgetc读取文件内容 ===\n"); while ((ch = fgetc(file)) != EOF) { putchar(ch); } rewind(file); printf("\n=== 使用fscanf读取格式化数据 ===\n"); char name[50]; int age; float score; fscanf(file, "姓名:%s\n", name); fscanf(file, "年龄:%d\n", &age); fscanf(file, "成绩:%f\n", &score); printf("姓名:%s\n", name); printf("年龄:%d\n", age); printf("成绩:%.2f\n", score); fclose(file); }
void binary_file_operations() { FILE *file; struct Person persons[] = { {"张三", 25, 175.5}, {"李四", 30, 180.0}, {"王五", 28, 172.3} }; int num_persons = sizeof(persons) / sizeof(persons[0]); printf("写入二进制文件...\n"); file = fopen("persons.dat", "wb"); if (file == NULL) { printf("无法打开文件!\n"); return; } fwrite(persons, sizeof(struct Person), num_persons, file); fclose(file); printf("读取二进制文件...\n"); file = fopen("persons.dat", "rb"); if (file == NULL) { printf("无法打开文件!\n"); return; } struct Person read_persons[3]; fread(read_persons, sizeof(struct Person), num_persons, file); for (int i = 0; i < num_persons; i++) { printf("\nPerson %d:\n", i + 1); printf("姓名:%s\n", read_persons[i].name); printf("年龄:%d\n", read_persons[i].age); printf("身高:%.2f\n", read_persons[i].height); } fclose(file); printf("\n追加到二进制文件...\n"); file = fopen("persons.dat", "ab"); if (file == NULL) { printf("无法打开文件!\n"); return; } struct Person new_person = {"赵六", 35, 178.8}; fwrite(&new_person, sizeof(struct Person), 1, file); fclose(file); printf("验证追加结果...\n"); file = fopen("persons.dat", "rb"); if (file == NULL) { printf("无法打开文件!\n"); return; } fseek(file, 0, SEEK_END); long file_size = ftell(file); int total_persons = file_size / sizeof(struct Person); rewind(file); struct Person *all_persons = (struct Person *)malloc(file_size); fread(all_persons, sizeof(struct Person), total_persons, file); for (int i = 0; i < total_persons; i++) { printf("\nPerson %d:\n", i + 1); printf("姓名:%s\n", all_persons[i].name); printf("年龄:%d\n", all_persons[i].age); printf("身高:%.2f\n", all_persons[i].height); } free(all_persons); fclose(file); }
void file_positioning() { FILE *file; char buffer[256]; file = fopen("test.txt", "r"); if (file == NULL) { printf("无法打开文件!\n"); return; } long position = ftell(file); printf("初始文件指针位置:%ld\n", position); fgets(buffer, sizeof(buffer), file); printf("读取的第一行:%s", buffer); position = ftell(file); printf("读取第一行后位置:%ld\n", position); fseek(file, 0, SEEK_SET); position = ftell(file); printf("移动到文件开头后位置:%ld\n", position); fgets(buffer, sizeof(buffer), file); printf("再次读取的第一行:%s", buffer); fseek(file, 0, SEEK_END); position = ftell(file); printf("移动到文件末尾后位置:%ld\n", position); fseek(file, -10, SEEK_CUR); position = ftell(file); printf("向前移动10个字节后位置:%ld\n", position); printf("从当前位置读取的内容:\n"); while (fgets(buffer, sizeof(buffer), file) != NULL) { printf("%s", buffer); } rewind(file); position = ftell(file); printf("rewind后文件指针位置:%ld\n", position); fclose(file); }
void error_handling() { FILE *file; char ch; printf("尝试打开一个不存在的文件:\n"); file = fopen("non_existent_file.txt", "r"); if (file == NULL) { perror("fopen"); printf("无法打开文件!\n"); } else { fclose(file); } printf("\n演示ferror和feof函数:\n"); file = fopen("test.txt", "r"); if (file == NULL) { perror("fopen"); return; } printf("读取文件内容:\n"); while ((ch = fgetc(file)) != EOF) { putchar(ch); } if (feof(file)) { printf("\n已到达文件末尾(feof返回非零值)\n"); } if (ferror(file)) { printf("读取文件时发生错误\n"); } else { printf("文件读取成功,无错误\n"); } clearerr(file); if (ferror(file)) { printf("错误标志未清除\n"); } else { printf("错误标志已清除\n"); } fclose(file); }
|