7
1
源代码
#include <stdio.h> #define N 80 #define M 100 typedef struct { char name[N]; // 书名 char author[N]; // 作者 } Book; void write(); void read(); int main() { printf("测试1: 把图书信息写入文本文件\n"); write(); printf("\n测试2: 从文本文件读取图书信息, 打印输出到屏幕\n"); read(); getchar(); getchar(); return 0; } void write() { Book x[] = { {"《雕塑家》", "斯科特.麦克劳德"}, {"《灯塔》", "克里斯多夫.夏布特"}, {"《人的局限性》", "塞缪尔.约翰生"}, {"《永不停步:玛格丽特.阿特伍德传》", "罗斯玛丽.沙利文"}, {"《大地之上》", "罗欣顿·米斯特里"}, {"《上学记》", "何兆武"}, {"《命运》", "蔡崇达"} }; int n, i; FILE *fp; // 计算数组x中元素个数 n = sizeof(x) / sizeof(x[0]); // 以写的方式打开文本文件data1.txt fp = fopen("data1.txt", "w"); // 如果打开文件失败,输出提示信息并返回 if(fp == NULL) { printf("fail to open file to write\n"); return; } // 将结构体数组x中的图书信息格式化写到fp指向的文件data1.txt for(i = 0; i < n; ++i) fprintf(fp, "%-40s %-20s\n", x[i].name, x[i].author); fclose(fp); } void read() { Book x[M]; int i, n; int number; FILE *fp; // 以读的方式打开文本文件data1.txt fp = fopen("data1.txt", "r"); // 如果打开文件失败,输出提示信息并返回 if(fp == NULL) { printf("fail to open file to read\n"); return; } // 从文件中读取图书信息,保存到结构体数组x中 i = 0; while(!feof(fp)) { number = fscanf(fp, "%s%s", x[i].name, x[i].author); if(number != 2) break; i++; } n = i; // 将图书信息打印输出到屏幕上 for(i = 0; i < n; ++i) printf("%d. %-40s%-20s\n", i+1, x[i].name, x[i].author); fclose(fp); }
结果
存在换行符,会多读取一次。
2
源代码
#include <stdio.h> #define N 80 #define M 100 typedef struct { char name[N]; // 书名 char author[N]; // 作者 } Book; void write(); void read(); int main() { printf("测试1: 把图书信息以数据块方式写入二进制文件\n"); write(); printf("\n测试2: 从二进制文件读取图书信息, 打印输出到屏幕\n"); read(); return 0; } void write() { Book x[] = { {"《雕塑家》", "斯科特.麦克劳德"}, {"《灯塔》", "克里斯多夫.夏布特"}, {"《人的局限性》", "塞缪尔.约翰生"}, {"《永不停步:玛格丽特.阿特伍德传》", "罗斯玛丽.沙利文"}, {"《大地之上》", "罗欣顿·米斯特里"}, {"《上学记》", "何兆武"}, {"《命运》", "蔡崇达"} }; int n; FILE *fp; // 计算数组x中元素个数 n = sizeof(x) / sizeof(x[0]); // 以写的方式打开二进制文件data2.dat fp = fopen("data2.dat", "wb"); // 如果打开文件失败,输出提示信息并返回 if(fp == NULL) { printf("fail to open file to write\n"); return; } // 将结构体数组x中的图书信息以数据块方式写入二进制文件data2.dat fwrite(x, sizeof(Book), n, fp); fclose(fp); } void read() { Book x[M]; int i, n; int number; FILE *fp; // 以读的方式打开二进制文件data2.dat fp = fopen("data2.dat", "rb"); // 如果打开文件失败,输出提示信息并返回 if(fp == NULL) { printf("fail to open file to read\n"); return; } // 从二进制文件data2.dat以数据块方式读取图书信息存储到结构体数组x i = 0; while(!feof(fp)) { number = fread(&x[i], sizeof(Book), 1, fp); if(number != 1) break; i++; } // 在屏幕上打印输出 n = i; for(i = 0; i < n; ++i) printf("%d. %-40s%-20s\n", i+1, x[i].name, x[i].author); fclose(fp); }
结果
存在换行符,会多读一次
3
#include <stdio.h> #define N 5 #define M 80 void write(); void read_str(); void read_char(); int main() { printf("测试1: 把一组字符信息以字符串方式写入文本文件\n"); write(); printf("\n测试2: 从文件以字符串方式读取, 输出到屏幕\n"); read_str(); printf("\n测试3: 从文件以单个字符方式读取, 输出到屏幕\n"); read_char(); return 0; } void write() { char *ptr[N] = { "Working\'s Blues", "Everything Will Flow", "Streets of London", "Perfect Day", "Philadelphia"}; int i; FILE *fp; fp = fopen("data3.txt", "w"); if(!fp) { printf("fail to open file to write\n"); return; } for(i = 0; i < N; ++i) { fputs(ptr[i], fp); fputs("\n", fp); } fclose(fp); } void read_str() { char songs[N][M]; int i; FILE *fp; fp = fopen("data3.txt", "r"); if(!fp) { printf("fail to open file to read\n"); return; } for(i = 0; i < N; ++i) fgets(songs[i], 80, fp); for(i = 0; i < N; ++i) printf("%d. %s", i+1, songs[i]); fclose(fp); } void read_char() { char ch; FILE *fp; fp = fopen("data3.txt", "r"); if(!fp) { printf("fail to open file to read\n"); return; } while(!feof(fp)) { ch = fgetc(fp); if(ch == EOF) break; putchar(ch); } fclose(fp); }
结果
其是转义字符
4
源代码
#include <stdio.h> #include <ctype.h> int main() { FILE *fp; char ch; int lines = 0; int words = 0; fp = fopen("data4.txt", "r"); if (fp == NULL) { perror("Fail to open file"); return 1; } while ((ch = fgetc(fp))!= EOF) { if (ch == '\n') { lines++; } if (!isspace(ch)) { words++; } } // 处理文件末尾没有换行符的情况 if (wordss> 0) { lines++; } fclose(fp); printf("data4.txt统计结果:\n"); printf("行数:%d\n", lines); printf("字符数(不计空白符):%d\n", characters); return 0;
结果
#include <stdio.h> #include <stdlib.h> #include <time.h> #define NUM 80 typedef struct { int id; char name[20]; char classes[20]; } Student; void readStudents(Student students[]) { FILE *fp; fp = fopen("d:\\list.txt", "r"); if (fp == NULL) { perror("打开文件失败"); exit(1); } for (int i = 0; i < NUM; i++) { fscanf(fp, "%d %s %s", &students[i].id, students[i].name, students[i].classes); } fclose(fp); } void randomSelect(Student students[]) { srand((unsigned int)time(NULL)); int selected[5]; for (int i = 0; i < 5; i++) { int index; do { index = rand() % NUM; } while (index == selected[0] || index == selected[1] || index == selected[2] || index == selected[3] || index == selected[4]); selected[i] = index; } printf("随机随机点名名单\n"); for (int i = 0; i < 5; i++) { printf("%d %s %s\n", students[selected[i]].id, students[selected[i]].name, students[selected[i]].classes); } char filename[50]; printf("请输入文件名:"); scanf("%s", filename); FILE *fp; fp = fopen(filename, "w"); if (fp == NULL) { perror("打开文件失败"); exit(1); } for (int i = 0; i < 5; i++) { fprintf(fp, "%d %s %s\n", students[selected[i]].id, students[selected[i]].name, students[selected[i]].classes); } fclose(fp); printf("保存成功!\n"); } int main() { Student students[NUM]; readStudents(students); randomSelect(students); return 0; }
结果