第9章 第3节:文件的读、写函数(练习)-《编程语言基础—C语言》(高教版第5版)同步精品课堂

2025-12-29
| 2份
| 31页
| 103人阅读
| 1人下载

内容正文:

9.3节:文件的读、写函数 一、单项选择题 1.以下代码片段的功能是( ) FILE *fp; int num; fp = fopen("data.txt", "w"); for(int i = 1; i <= 10; i++) { fprintf(fp, "%d ", i); } fclose(fp); A. 从文件中读取10个整数 B. 向文件中写入1到10的整数,每行一个 C. 计算1到10的和并写入文件 D. 从键盘输入10个整数并写入文件 2.以下代码片段的功能是() struct Student { char name[20]; int age; float score; }; FILE *fp; struct Student stu; fp = fopen("stu.dat", "rb"); fread(&stu, sizeof(struct Student), 1, fp); fclose(fp); A. 向文件中写入一个学生信息 B. 从文件中读取一个学生信息 C. 修改文件中的学生信息 D. 删除文件中的学生信息 3.以下代码片段的功能是() struct Student { char name[20]; int age; float score; }; FILE *fp; struct Student stu = {"张三", 20, 85.5}; fp = fopen("stu.dat", "wb"); fwrite(&stu, sizeof(struct Student), 1, fp); fclose(fp); A. 向文件中写入一个学生信息 B. 从文件中读取一个学生信息 C. 修改文件中的学生信息 D. 删除文件中的学生信息 4.以下关于文件操作的描述,错误的是() A. 使用fopen函数打开文件后必须检查返回值是否为NULL B. 文件操作完成后必须调用fclose函数关闭文件 C. 可以使用fgetc函数读取二进制文件 D. 使用fread函数读取文本文件会出错 5.以下代码片段的功能是() FILE *fp1, *fp2; char ch; fp1 = fopen("file1.txt", "r"); fp2 = fopen("file2.txt", "w"); while((ch = fgetc(fp1)) != EOF) { fputc(ch, fp2); } fclose(fp1); fclose(fp2); A. 将file1.txt的内容复制到file2.txt B. 将file2.txt的内容复制到file1.txt C. 比较两个文件的内容 D. 合并两个文件的内容 二、程序运行结果题 6.执行以下程序的运行结果是(假设文件test.txt内容为"Hello World ")() #include <stdio.h> main() { FILE *fp; char ch; fp = fopen("test.txt", "r"); if(fp == NULL) { printf("文件打开失败 "); return 1; } while((ch = fgetc(fp)) != EOF) { putchar(ch); } fclose(fp); } 7.执行以下程序的运行结果是(假设文件test.txt内容为"Hello World ")() #include <stdio.h> main() { FILE *fp; char str[100]; fp = fopen("test.txt", "r"); if(fp == NULL) { printf("文件打开失败 "); return 1; } while(fgets(str, 100, fp) != NULL) { printf("%s", str); } fclose(fp); } 8.以下程序执行后,文件data.txt的内容是() #include <stdio.h> main() { FILE *fp; int i; fp = fopen("data.txt", "w"); if(fp == NULL) { printf("文件打开失败 "); return 1; } for(i = 1; i <= 5; i++) { fprintf(fp, "%d ", i * i); } fclose(fp); } 9.以下程序执行后,文件data.txt的内容是() #include <stdio.h> main() { FILE *fp; char *str[] = {"Hello", "World", "C", "Programming"}; int i; fp = fopen("data.txt", "w"); if(fp == NULL) { printf("文件打开失败 "); return 1; } for(i = 0; i < 4; i++) { fputs(str[i], fp); fputc(' ', fp); } fclose(fp); } 10.以下程序执行后,文件data.bin的内容是() #include <stdio.h> main() { FILE *fp; int arr[] = {10, 20, 30, 40, 50}; fp = fopen("data.bin", "wb"); if(fp == NULL) { printf("文件打开失败 "); return 1; } fwrite(arr, sizeof(int), 5, fp); fclose(fp); } 11.执行以下程序的运行结果是(设data.bin内容为:10 20 30 40 50)() #include <stdio.h> main() { FILE *fp; int arr[5]; int i; fp = fopen("data.bin", "rb"); if(fp == NULL) { printf("文件打开失败 "); return 1; } fread(arr, sizeof(int), 5, fp); fclose(fp); for(i = 0; i < 5; i++) { printf("%d ", arr[i]); } } 12.以下程序执行后,文件student.dat的内容是() #include <stdio.h> struct Student { char name[20]; int age; float score; }; main() { FILE *fp; struct Student stu = {"张三", 20, 85.5}; fp = fopen("student.dat", "wb"); if(fp == NULL) { printf("文件打开失败 "); return 1; } fwrite(&stu, sizeof(struct Student), 1, fp); fclose(fp); } 13.以下程序执行后,屏幕输出结果是(设float.dat内容:3.14159 2.71828)() #include <stdio.h> main() { FILE *fp; float f1, f2; fp = fopen("float.dat", "r"); if(fp == NULL) { printf("文件打开失败 "); return 1; } fscanf(fp, "%f %f", &f1, &f2); fclose(fp); printf("f1 = %.5f, f2 = %.5f ", f1, f2); } 14.以下程序执行后,屏幕输出结果是(设文件test.txt内容为"ABCDEFG 12345 ")() #include <stdio.h> main() { FILE *fp; char ch; int count = 0; fp = fopen("test.txt", "r"); if(fp == NULL) { printf("文件打开失败 "); return 1; } while((ch = fgetc(fp)) != EOF) { if(ch >= 'A' && ch <= 'Z') { count++; } } fclose(fp); printf("大写字母数量:%d ", count); } 15.以下程序执行后,文件result.txt的内容是() #include <stdio.h> main() { FILE *fp; int i; fp = fopen("result.txt", "w"); if(fp == NULL) { printf("文件打开失败 "); return 1; } for(i = 1; i <= 10; i++) { if(i % 2 == 0) { fprintf(fp, "%d是偶数 ", i); } } fclose(fp); } 16.以下程序执行后,屏幕输出结果是(假设文件numbers.txt内容为"10 20 30 40 50")() #include <stdio.h> main() { FILE *fp; int num, sum = 0; fp = fopen("numbers.txt", "r"); if(fp == NULL) { printf("文件打开失败 "); return 1; } while(fscanf(fp, "%d", &num) == 1) { sum += num; } fclose(fp); printf("总和:%d ", sum); } 17.以下程序执行后,文件output.txt的内容是() #include <stdio.h> #include <string.h> main() { FILE *fp; char *words[] = {"apple", "banana", "cherry", "date"}; int i; fp = fopen("output.txt", "w"); if(fp == NULL) { printf("文件打开失败 "); return 1; } for(i = 0; i < 4; i++) { fprintf(fp, "%s: %d个字符 ", words[i], (int)strlen(words[i])); } fclose(fp); } 18.以下程序执行后,文件data.txt的内容是() #include <stdio.h> main() { FILE *fp; char ch; fp = fopen("data.txt", "w"); if(fp == NULL) { printf("文件打开失败 "); return 1; } for(ch = 'A'; ch <= 'E'; ch++) { fputc(ch, fp); fputc(' ', fp); } fclose(fp); } 19.以下程序执行后,文件output.txt的内容是() #include <stdio.h> main() { FILE *fp; char *str[] = {"Hello", "World", "C", "Programming"}; int i; fp = fopen("output.txt", "w"); if(fp == NULL) { printf("文件打开失败 "); return 1; } for(i = 0; i < 4; i++) { fputs(str[i], fp); if(i < 3) { fputs(" | ", fp); } } fclose(fp); } 20.以下程序执行后,文件student.dat的内容是() #include <stdio.h> struct Student { char name[20]; int age; float score; }; main() { FILE *fp; struct Student stu = {"李四", 21, 92.5}; fp = fopen("student.dat", "wb"); if(fp == NULL) { printf("文件打开失败 "); return 1; } fwrite(&stu, sizeof(struct Student), 1, fp); fclose(fp); } 三、编程题 1. 从键盘输入字符并写入文件 2.复制文件内容到另一个文件 3.将字符串写入文件并读取显示 4.统计文件中的字符类型 原创精品资源学科网独家享有版权,侵权必究! 学科网(北京)股份有限公司 $ 9.3节:文件的读、写函数 一、单项选择题 1.以下代码片段的功能是( ) FILE *fp; int num; fp = fopen("data.txt", "w"); for(int i = 1; i <= 10; i++) { fprintf(fp, "%d ", i); } fclose(fp); A. 从文件中读取10个整数 B. 向文件中写入1到10的整数,每行一个 C. 计算1到10的和并写入文件 D. 从键盘输入10个整数并写入文件 【答案】B 2.以下代码片段的功能是( ) struct Student { char name[20]; int age; float score; }; FILE *fp; struct Student stu; fp = fopen("stu.dat", "rb"); fread(&stu, sizeof(struct Student), 1, fp); fclose(fp); A. 向文件中写入一个学生信息 B. 从文件中读取一个学生信息 C. 修改文件中的学生信息 D. 删除文件中的学生信息 【答案】B 3.以下代码片段的功能是( ) struct Student { char name[20]; int age; float score; }; FILE *fp; struct Student stu = {"张三", 20, 85.5}; fp = fopen("stu.dat", "wb"); fwrite(&stu, sizeof(struct Student), 1, fp); fclose(fp); A. 向文件中写入一个学生信息 B. 从文件中读取一个学生信息 C. 修改文件中的学生信息 D. 删除文件中的学生信息 【答案】A 4.以下关于文件操作的描述,错误的是( ) A. 使用fopen函数打开文件后必须检查返回值是否为NULL B. 文件操作完成后必须调用fclose函数关闭文件 C. 可以使用fgetc函数读取二进制文件 D. 使用fread函数读取文本文件会出错 【答案】D 5.以下代码片段的功能是( ) FILE *fp1, *fp2; char ch; fp1 = fopen("file1.txt", "r"); fp2 = fopen("file2.txt", "w"); while((ch = fgetc(fp1)) != EOF) { fputc(ch, fp2); } fclose(fp1); fclose(fp2); A. 将file1.txt的内容复制到file2.txt B. 将file2.txt的内容复制到file1.txt C. 比较两个文件的内容 D. 合并两个文件的内容 【答案】A 二、程序运行结果题 6.执行以下程序的运行结果是(假设文件test.txt内容为"Hello World ")( ) #include <stdio.h> main() { FILE *fp; char ch; fp = fopen("test.txt", "r"); if(fp == NULL) { printf("文件打开失败 "); return 1; } while((ch = fgetc(fp)) != EOF) { putchar(ch); } fclose(fp); } 【答案】Hello World 7.执行以下程序的运行结果是(假设文件test.txt内容为"Hello World ")( ) #include <stdio.h> main() { FILE *fp; char str[100]; fp = fopen("test.txt", "r"); if(fp == NULL) { printf("文件打开失败 "); return 1; } while(fgets(str, 100, fp) != NULL) { printf("%s", str); } fclose(fp); } 【答案】Hello World 8.以下程序执行后,文件data.txt的内容是( ) #include <stdio.h> main() { FILE *fp; int i; fp = fopen("data.txt", "w"); if(fp == NULL) { printf("文件打开失败 "); return 1; } for(i = 1; i <= 5; i++) { fprintf(fp, "%d ", i * i); } fclose(fp); } 【答案】 1 4 9 16 25 9.以下程序执行后,文件data.txt的内容是( ) #include <stdio.h> main() { FILE *fp; char *str[] = {"Hello", "World", "C", "Programming"}; int i; fp = fopen("data.txt", "w"); if(fp == NULL) { printf("文件打开失败 "); return 1; } for(i = 0; i < 4; i++) { fputs(str[i], fp); fputc(' ', fp); } fclose(fp); } 【答案】 Hello World C Programming 10.以下程序执行后,文件data.bin的内容是( ) #include <stdio.h> main() { FILE *fp; int arr[] = {10, 20, 30, 40, 50}; fp = fopen("data.bin", "wb"); if(fp == NULL) { printf("文件打开失败 "); return 1; } fwrite(arr, sizeof(int), 5, fp); fclose(fp); } 【答案】文件中存储的是5个整数的二进制数据:10, 20, 30, 40, 50 11.执行以下程序的运行结果是(设data.bin内容为:10 20 30 40 50)( ) #include <stdio.h> main() { FILE *fp; int arr[5]; int i; fp = fopen("data.bin", "rb"); if(fp == NULL) { printf("文件打开失败 "); return 1; } fread(arr, sizeof(int), 5, fp); fclose(fp); for(i = 0; i < 5; i++) { printf("%d ", arr[i]); } } 【答案】10 20 30 40 50 12.以下程序执行后,文件student.dat的内容是( ) #include <stdio.h> struct Student { char name[20]; int age; float score; }; main() { FILE *fp; struct Student stu = {"张三", 20, 85.5}; fp = fopen("student.dat", "wb"); if(fp == NULL) { printf("文件打开失败 "); return 1; } fwrite(&stu, sizeof(struct Student), 1, fp); fclose(fp); } 【答案】文件中存储的是一个学生结构体的二进制数据:姓名"张三"、年龄20、成绩85.5 13.以下程序执行后,屏幕输出结果是(设float.dat内容:3.14159 2.71828)( ) #include <stdio.h> main() { FILE *fp; float f1, f2; fp = fopen("float.dat", "r"); if(fp == NULL) { printf("文件打开失败 "); return 1; } fscanf(fp, "%f %f", &f1, &f2); fclose(fp); printf("f1 = %.5f, f2 = %.5f ", f1, f2); } 【答案】f1 = 3.14159, f2 = 2.71828 14.以下程序执行后,屏幕输出结果是(设文件test.txt内容为"ABCDEFG 12345 ")( ) #include <stdio.h> main() { FILE *fp; char ch; int count = 0; fp = fopen("test.txt", "r"); if(fp == NULL) { printf("文件打开失败 "); return 1; } while((ch = fgetc(fp)) != EOF) { if(ch >= 'A' && ch <= 'Z') { count++; } } fclose(fp); printf("大写字母数量:%d ", count); } 【答案】大写字母数量:7 15.以下程序执行后,文件result.txt的内容是( ) #include <stdio.h> main() { FILE *fp; int i; fp = fopen("result.txt", "w"); if(fp == NULL) { printf("文件打开失败 "); return 1; } for(i = 1; i <= 10; i++) { if(i % 2 == 0) { fprintf(fp, "%d是偶数 ", i); } } fclose(fp); } 【答案】 2是偶数 4是偶数 6是偶数 8是偶数 10是偶数 16.以下程序执行后,屏幕输出结果是(假设文件numbers.txt内容为"10 20 30 40 50")( ) #include <stdio.h> main() { FILE *fp; int num, sum = 0; fp = fopen("numbers.txt", "r"); if(fp == NULL) { printf("文件打开失败 "); return 1; } while(fscanf(fp, "%d", &num) == 1) { sum += num; } fclose(fp); printf("总和:%d ", sum); } 【答案】总和:150 17.以下程序执行后,文件output.txt的内容是( ) #include <stdio.h> #include <string.h> main() { FILE *fp; char *words[] = {"apple", "banana", "cherry", "date"}; int i; fp = fopen("output.txt", "w"); if(fp == NULL) { printf("文件打开失败 "); return 1; } for(i = 0; i < 4; i++) { fprintf(fp, "%s: %d个字符 ", words[i], (int)strlen(words[i])); } fclose(fp); } 【答案】 apple: 5个字符 banana: 6个字符 cherry: 6个字符 date: 4个字符 18.以下程序执行后,文件data.txt的内容是( ) #include <stdio.h> main() { FILE *fp; char ch; fp = fopen("data.txt", "w"); if(fp == NULL) { printf("文件打开失败 "); return 1; } for(ch = 'A'; ch <= 'E'; ch++) { fputc(ch, fp); fputc(' ', fp); } fclose(fp); } 【答案】 A B C D E 19.以下程序执行后,文件output.txt的内容是( ) #include <stdio.h> main() { FILE *fp; char *str[] = {"Hello", "World", "C", "Programming"}; int i; fp = fopen("output.txt", "w"); if(fp == NULL) { printf("文件打开失败 "); return 1; } for(i = 0; i < 4; i++) { fputs(str[i], fp); if(i < 3) { fputs(" | ", fp); } } fclose(fp); } 【答案】Hello | World | C | Programming 20.以下程序执行后,文件student.dat的内容是( ) #include <stdio.h> struct Student { char name[20]; int age; float score; }; main() { FILE *fp; struct Student stu = {"李四", 21, 92.5}; fp = fopen("student.dat", "wb"); if(fp == NULL) { printf("文件打开失败 "); return 1; } fwrite(&stu, sizeof(struct Student), 1, fp); fclose(fp); } 【答案】文件中存储的是一个学生结构体的二进制数据:姓名"李四"、年龄21、成绩92.5 三、编程题 1. 从键盘输入字符并写入文件 【答案】 #include <stdio.h> main() { FILE *fp; char ch; fp = fopen("file1.dat", "w"); if(fp == NULL) { printf("文件打开失败 "); return 1; } printf("请输入字符(以#结束): "); while((ch = getchar()) != '#') { fputc(ch, fp); } fclose(fp); printf("数据已写入文件file1.dat "); } 2.复制文件内容到另一个文件 【答案】 #include <stdio.h> main() { FILE *fp1, *fp2; char ch; fp1 = fopen("file1.dat", "r"); if(fp1 == NULL) { printf("源文件打开失败 "); return 1; } fp2 = fopen("file2.dat", "w"); if(fp2 == NULL) { printf("目标文件打开失败 "); fclose(fp1); return 1; } while((ch = fgetc(fp1)) != EOF) { fputc(ch, fp2); } fclose(fp1); fclose(fp2); printf("文件复制完成 "); } 3.将字符串写入文件并读取显示 【答案】 #include <stdio.h> #include <string.h> main() { FILE *fp; char str[100]; char buffer[100]; int i; fp = fopen("fa.txt", "w"); if(fp == NULL) { printf("文件打开失败 "); return 1; } printf("请输入3个字符串: "); for(i = 0; i < 3; i++) { printf("字符串%d:", i+1); fgets(str, 100, stdin); fputs(str, fp); } fclose(fp); // 读取文件内容 fp = fopen("fa.txt", "r"); if(fp == NULL) { printf("文件打开失败 "); return 1; } printf(" 文件内容: "); while(fgets(buffer, 100, fp) != NULL) { printf("%s", buffer); } fclose(fp); } 4.统计文件中的字符类型 【答案】 #include <stdio.h> #include <ctype.h> main() { FILE *fp; char ch; int letters = 0, digits = 0, spaces = 0, others = 0; fp = fopen("text.txt", "r"); if(fp == NULL) { printf("文件打开失败 "); return 1; } while((ch = fgetc(fp)) != EOF) { if(isalpha(ch)) { letters++; } else if(isdigit(ch)) { digits++; } else if(isspace(ch)) { spaces++; } else { others++; } } fclose(fp); printf("统计结果: "); printf("字母:%d ", letters); printf("数字:%d ", digits); printf("空格:%d ", spaces); printf("其他:%d ", others); } 原创精品资源学科网独家享有版权,侵权必究! 学科网(北京)股份有限公司 $

资源预览图

第9章 第3节:文件的读、写函数(练习)-《编程语言基础—C语言》(高教版第5版)同步精品课堂
1
第9章 第3节:文件的读、写函数(练习)-《编程语言基础—C语言》(高教版第5版)同步精品课堂
2
第9章 第3节:文件的读、写函数(练习)-《编程语言基础—C语言》(高教版第5版)同步精品课堂
3
所属专辑
由于学科网是一个信息分享及获取的平台,不确保部分用户上传资料的 来源及知识产权归属。如您发现相关资料侵犯您的合法权益,请联系学科网,我们核实后将及时进行处理。