C程序设计(第5版)谭浩强习题解答 第10章 对文件的输入输出

2024-02-07 23:04

本文主要是介绍C程序设计(第5版)谭浩强习题解答 第10章 对文件的输入输出,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

C程序设计(第5版)谭浩强习题解答

第10章 对文件的输入输出

1.什么是文件型指针?通过文件指针访问文件有什么好处?

缓冲文件系统中,关键的概念是“文件类型指针”,简称“文件指针”。每个被使用的文件都在内存中开辟一个相应的文件信息区,用来存放文件的有关信息(如文件的名字、文件状态及文件当前位置等)。这些信息是保存在一个结构体变量中的。该结构体类型是由系统声明的,取名为FILE。
通过文件指针访问文件的好处是:可以随机访问文件,有效表示数据结构,动态分配内存,方便使用字符串,有效使用数组。

2.对文件的打开与关闭的含义是什么?为什么要打开和关闭文件?

答:”打开“是指为文件建立相应的信息区(用来存放有关文件的信息)和文件缓冲区(用来暂时存放输人输出的数据)。
”关闭“是指撤销文件信息区和文件缓冲区,使文件指针变量不再指向该文件,显然就无法进行对文件的读写了。

3.从键盘输入一个字符串,将其中的小写字母全部转换成大写字母,然后输出到一个磁盘文件test中保存,输入的字符串以“!”结束。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{FILE *fp;char str[100];int i = 0;if ((fp = fopen("a1", "w")) == NULL){printf("can not open file\n");exit(0);}printf("input a string:\n");gets(str);while (str[i] != '!'){if (str[i] >= 'a'&& str[i] <= 'z')str[i] = str[i] - 32;fputc(str[i], fp);i++;}fclose(fp);fp = fopen("a1", "r");fgets(str, strlen(str) + 1, fp);printf("%s\n", str);fclose(fp);return 0;
}

4.有两个磁盘文件A和B,各存放一行字母,今要求把这两个文件中的信息合并(按字母顺序排列),输出到一个新文件C中去。

#include <stdio.h>
#include <stdlib.h>
int main()
{FILE *fp;int i, j, n, i1;char c[100], t, ch;if ((fp = fopen("a1", "r")) == NULL){printf("\ncan not open file\n");exit(0);}printf("file A :\n");for (i = 0; (ch = fgetc(fp)) != EOF; i++){c[i] = ch;putchar(c[i]);}fclose(fp);i1 = i;if ((fp = fopen("b1", "r")) == NULL){printf("\ncan not open file\n");exit(0);}printf("\nfile B:\n");for (i = i1; (ch = fgetc(fp)) != EOF; i++){c[i] = ch;putchar(c[i]);}fclose(fp);n = i;for (i = 0; i < n; i++)for (j = i + 1; j < n; j++)if (c[i] > c[j]){t = c[i];c[i] = c[j];c[j] = t;}printf("\nfile C :\n");fp = fopen("c1", "w");for (i = 0; i < n; i++){putc(c[i], fp);putchar(c[i]);}printf("\n");fclose(fp);return 0;
}

5.有5个学生,每个学生有3门课程的成绩,从键盘输人学生数据(包括学号,姓名,3门课程成绩),计算出平均成绩,将原有数据和计算出的平均分数存放在磁盘文件stud中。

//10.5.1
#include <stdio.h>
struct student
{char num[10];char name[8];int score[3];float ave;
}  stu[5];int main()
{int i, j, sum;FILE *fp;for (i = 0; i < 5; i++){printf("\ninput score of student %d:\n", i + 1);printf("NO.:");scanf("%s", stu[i].num);printf("name:");scanf("%s", stu[i].name);sum = 0;for (j = 0; j < 3; j++){printf("score %d:", j + 1);scanf("%d", &stu[i].score[j]);sum += stu[i].score[j];}stu[i].ave = sum / 3.0;}//将数据写入文件fp = fopen("stud", "w");for (i = 0; i < 5; i++)if (fwrite(&stu[i], sizeof(struct student), 1, fp) != 1)printf("file write error\n");fclose(fp);fp = fopen("stud", "r");for (i = 0; i < 5; i++){fread(&stu[i], sizeof(struct student), 1, fp);printf("\n%s,%s,%d,%d,%d,%6.2f\n", stu[i].num, stu[i].name, stu[i].score[0],stu[i].score[1], stu[i].score[2], stu[i].ave);}return 0;
}//10.5.2
#include <stdio.h>
#define SIZE 5
struct student
{char name[10];int num;int score[3];float ave;
}  stud[SIZE];int main()
{void save(void);int i;float sum[SIZE];FILE *fp1;for (i = 0; i < SIZE; i++){scanf("%s %d %d %d %d", stud[i].name, &stud[i].num, &stud[i].score[0],&stud[i].score[1], &stud[i].score[2]);sum[i] = stud[i].score[0] + stud[i].score[1] + stud[i].score[2];stud[i].ave = sum[i] / 3;}save();fp1 = fopen("stu.dat", "rb");printf("\n name      NO.    score1  score2  score3   ave\n");printf("-----------------------------------------------\n");for (i = 0; i < SIZE; i++){fread(&stud[i], sizeof(struct student), 1, fp1);printf("%-10s %3d %7d %7d %7d %8.2f\n", stud[i].name, stud[i].num,stud[i].score[0], stud[i].score[1], stud[i].score[2], stud[i].ave);}fclose(fp1);return 0;
}void save(void)
{FILE *fp;int i;if ((fp = fopen("stu.dat", "wb")) == NULL){printf("The file can not open\n");return;}for (i = 0; i < SIZE; i++)if (fwrite(&stud[i], sizeof(struct student), 1, fp) != 1){printf("file write error\n");return;}fclose(fp);
}

6.将第5题stud文件中的学生数据,按平均分进行排序处理,将已排序的学生数据存入一个新文件 stu sort中。

//10.6.1
#include <stdio.h>
#include <stdlib.h>
#define N 10
struct student
{char num[10];char name[8];int score[3];float ave;
} st[N], temp;int main()
{FILE *fp;int i, j, n;//读文件if ((fp = fopen("stud", "r")) == NULL){printf("can not open.\n");exit(0);}printf("File 'stud': ");for (i = 0; fread(&st[i], sizeof(struct student), 1, fp) != 0; i++){printf("\n%8s%8s", st[i].num, st[i].name);for (j = 0; j < 3; j++)printf("%8d", st[i].score[j]);printf("%10.2f", st[i].ave);}printf("\n");fclose(fp);n = i;//排序for (i = 0; i < n; i++)for (j = i + 1; j < n; j++)if (st[i].ave < st[j].ave){temp = st[i];st[i] = st[j];st[j] = temp;}//输出printf("\nNow:");fp = fopen("stu_sort", "w");for (i = 0; i < n; i++){fwrite(&st[i], sizeof(struct student), 1, fp);printf("\n%8s%8s", st[i].num, st[i].name);for (j = 0; j < 3; j++)printf("%8d", st[i].score[j]);printf("%10.2f", st[i].ave);}printf("\n");fclose(fp);return 0;
}//10.6.2
#include <stdio.h>
#include <stdlib.h>
#define SIZE 5
struct student
{char name[10];int num;int score[3];float ave;
}  stud[SIZE], work;
int main()
{void sort(void);int i;FILE *fp;sort();fp = fopen("stud_sort.dat", "rb");printf("sorted student's scores list as follow\n");printf("----------------------------------------------------\n");printf(" NAME      N0.     SCORE1   SCORE2   SCORE3    AVE    \n");printf("----------------------------------------------------\n");for (i = 0; i < SIZE; i++){fread(&stud[i], sizeof(struct student), 1, fp);printf("%-10s %3d %8d %8d %8d %9.2f\n", stud[i].name, stud[i].num,stud[i].score[0], stud[i].score[1], stud[i].score[2], stud[i].ave);}fclose(fp);return 0;
}void sort(void)
{FILE *fp1, *fp2;int i, j;if ((fp1 = fopen("stu.dat", "rb")) == NULL){printf("The file can not open\n\n");exit(0);}if ((fp2 = fopen("stud_sort.dat", "wb")) == NULL){printf("The file write error\n");exit(0);}for (i = 0; i < SIZE; i++)if (fread(&stud[i], sizeof(struct student), 1, fp1) != 1){printf("file read error\n");exit(0);}for (i = 0; i < SIZE; i++){for (j = i + 1; j < SIZE; j++)if (stud[i].ave < stud[j].ave){work = stud[i];stud[i] = stud[j];stud[j] = work;}fwrite(&stud[i], sizeof(struct student), 1, fp2);}fclose(fp1);fclose(fp2);
}

7.将第6题已排序的学生成绩文件进行插入处理。插入一个学生的3门课程成绩,程序先计算新插人学生的平均成绩,然后将它按成绩高低顺序插人,插入后建立一个新文件stu_sort中。

#include <stdio.h>
#include <stdlib.h>
struct student
{char num[10];char name[8];int score[3];float ave;
}  st[10], s;int main()
{FILE *fp, *fp1;int i, j, t, n;printf("\nNO.:");scanf("%s", s.num);printf("name:");scanf("%s", s.name);printf("score1,score2,score3:");scanf("%d,%d,%d", &s.score[0], &s.score[1], &s.score[2]);s.ave = (s.score[0] + s.score[1] + s.score[2]) / 3.0;//从文件读数据if ((fp = fopen("stu_sort", "r")) == NULL){printf("can not open file.");exit(0);}printf("original data:\n");for (i = 0; fread(&st[i], sizeof(struct student), 1, fp) != 0; i++){printf("\n%8s%8s", st[i].num, st[i].name);for (j = 0; j < 3; j++)printf("%8d", st[i].score[j]);printf("%10.2f", st[i].ave);}n = i;for (t = 0; st[t].ave > s.ave && t < n; t++);//向文件写数据printf("\nNow:\n");fp1 = fopen("sort1.dat", "w");for (i = 0; i < t; i++){fwrite(&st[i], sizeof(struct student), 1, fp1);printf("\n %8s%8s", st[i].num, st[i].name);for (j = 0; j < 3; j++)printf("%8d", st[i].score[j]);printf("%10.2f", st[i].ave);}fwrite(&s, sizeof(struct student), 1, fp1);printf("\n %8s %7s %7d %7d %7d%10.2f", s.num, s.name, s.score[0],s.score[1], s.score[2], s.ave);for (i = t; i < n; i++){fwrite(&st[i], sizeof(struct student), 1, fp1);printf("\n %8s%8s", st[i].num, st[i].name);for (j = 0; j < 3; j++)printf("%8d", st[i].score[j]);printf("%10.2f", st[i].ave);}printf("\n");fclose(fp);fclose(fp1);return 0;
}

8.将第7题结果仍存人原有的stusort文件而不另建立新文件。

#include <stdio.h>
#include <stdlib.h>
struct student
{char num[10];char name[8];int score[3];float ave;
}  st[10], s;int main()
{FILE *fp, *fp1;int i, j, t, n;printf("\nNO.:");scanf("%s", s.num);printf("name:");scanf("%s", s.name);printf("score1,score2,score3:");scanf("%d,%d,%d", &s.score[0], &s.score[1], &s.score[2]);s.ave = (s.score[0] + s.score[1] + s.score[2]) / 3.0;//从文件读数据if ((fp = fopen("stu_sort", "r")) == NULL){printf("can not open file.");exit(0);}printf("original data:\n");for (i = 0; fread(&st[i], sizeof(struct student), 1, fp) != 0; i++){printf("\n%8s%8s", st[i].num, st[i].name);for (j = 0; j < 3; j++)printf("%8d", st[i].score[j]);printf("%10.2f", st[i].ave);}n = i;for (t = 0; st[t].ave > s.ave && t < n; t++);//向文件写数据printf("\nNow:\n");fp1 = fopen("sort1.dat", "w");for (i = 0; i < t; i++){fwrite(&st[i], sizeof(struct student), 1, fp1);printf("\n %8s%8s", st[i].num, st[i].name);for (j = 0; j < 3; j++)printf("%8d", st[i].score[j]);printf("%10.2f", st[i].ave);}fwrite(&s, sizeof(struct student), 1, fp1);printf("\n %8s %7s %7d %7d %7d%10.2f", s.num, s.name, s.score[0],s.score[1], s.score[2], s.ave);for (i = t; i < n; i++){fwrite(&st[i], sizeof(struct student), 1, fp1);printf("\n %8s%8s", st[i].num, st[i].name);for (j = 0; j < 3; j++)printf("%8d", st[i].score[j]);printf("%10.2f", st[i].ave);}printf("\n");fclose(fp);fclose(fp1);return 0;
}

9.有一磁盘文件employee,内存放职工的数据。每个职工的数据包括职工姓名、职工号、性别、年龄、住址、工资、健康状况、文化程度。今要求将职工名、工资的信息单独抽出来另建一个简明的职工工资文件。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct emploee
{char    num[6];char    name[10];char    sex[2];int     age;char    addr[20];int     salary;char    health[8];char    class[10];
}em[10];struct emp
{char name[10];int  salary;
}em_case[10];int main()
{FILE *fp1, *fp2;int i, j;if ((fp1 = fopen("emploee", "r")) == NULL){printf("can not open file.\n");exit(0);}printf("\n  NO.   name  sex   age    addr   salary   health  class\n");for (i = 0; fread(&em[i], sizeof(struct emploee), 1, fp1) != 0; i++){printf("\n%4s%8s%4s%6d%10s%6d%10s%8s", em[i].num, em[i].name, em[i].sex,em[i].age, em[i].addr, em[i].salary, em[i].health, em[i].class);strcpy(em_case[i].name, em[i].name);em_case[i].salary = em[i].salary;}printf("\n\n  ********************************      ");if ((fp2 = fopen("emp_salary", "wb")) == NULL){printf("can not open file\n");exit(0);}for (j = 0; j < i; j++){if (fwrite(&em_case[j], sizeof(struct emp), 1, fp2) != 1)printf("error!");printf("\n  %12s%10d", em_case[j].name, em_case[j].salary);}printf("\n   *******************************     ");fclose(fp1);fclose(fp2);return 0;
}

10.从第9题的“职工工资文件”中删去一个职工的数据,再存回原文件。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct emploee
{char  name[10];int   salary;
}emp[20];int main()
{FILE *fp;int i, j, n, flag;char name[10];if ((fp = fopen("emp_salary", "rb")) == NULL){printf("can not open file.\n");exit(0);}printf("\noriginal data:\n");for (i = 0; fread(&emp[i], sizeof(struct emploee), 1, fp) != 0; i++)printf("\n  %8s   %7d", emp[i].name, emp[i].salary);fclose(fp);n = i;printf("\ninput name deleted:\n");scanf("%s", name);for (flag = 1, i = 0; flag && i < n; i++){if (strcmp(name, emp[i].name) == 0){for (j = i; j < n - 1; j++){strcpy(emp[j].name, emp[j + 1].name);emp[j].salary = emp[j + 1].salary;}flag = 0;}}if (!flag)n = n - 1;elseprintf("\nnot found!");printf("\nNow,The content of file:\n");if ((fp = fopen("emp_salary", "wb")) == NULL){printf("can not open file\n");exit(0);}for (i = 0; i < n; i++)fwrite(&emp[i], sizeof(struct emploee), 1, fp);fclose(fp);fp = fopen("emp_salary", "r");for (i = 0; fread(&emp[i], sizeof(struct emploee), 1, fp) != 0; i++)printf("\n%8s   %7d", emp[i].name, emp[i].salary);printf("\n");fclose(fp);return 0;
}

11.从键盘输入若干行字符(每行长度不等),输入后把它们存储到一磁盘文件中。再从该文件中读入这些数据,将其中小写字母转换成大写字母后在显示屏上输出。

#include <stdio.h>
int main()
{int i, flag;char str[80], c;FILE *fp;fp = fopen("text", "w");flag = 1;while (flag == 1){printf("input string:\n");gets(str);fprintf(fp, "%s ", str);printf("continue?");c = getchar();if ((c == 'N') || (c == 'n'))flag = 0;getchar();}fclose(fp);fp = fopen("text", "r");while (fscanf(fp, "%s", str) != EOF){for (i = 0; str[i] != '\0'; i++)if ((str[i] >= 'a') && (str[i] <= 'z'))str[i] -= 32;printf("%s\n", str);}fclose(fp);return 0;
}

这篇关于C程序设计(第5版)谭浩强习题解答 第10章 对文件的输入输出的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/689176

相关文章

【C++ Primer Plus习题】13.4

大家好,这里是国中之林! ❥前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。有兴趣的可以点点进去看看← 问题: 解答: main.cpp #include <iostream>#include "port.h"int main() {Port p1;Port p2("Abc", "Bcc", 30);std::cout <<

C语言程序设计(数据类型、运算符与表达式)

一、C的数据类型 C语言提供的数据类型: 二、常量和变量 2.1常量和符号常量 在程序运行过程中,其值不能被改变的量称为常量。 常量区分为不同的类型: 程序中用#define(预处理器指令)命令行定义变量将代表常量,用一个标识符代表一个常量,称为符合常量。 2.2变量 变量代表内存中具有特定属性的一个存储单元,用来存放数据,在程序运行期间,这些值是可以 改变的。 变

C语言程序设计(选择结构程序设计)

一、关系运算符和关系表达式 1.1关系运算符及其优先次序 ①<(小于) ②<=(小于或等于) ③>(大于) ④>=(大于或等于 ) ⑤==(等于) ⑥!=(不等于) 说明: 前4个优先级相同,后2个优先级相同,关系运算符的优先级低于算术运算符,关系运算符的优先级高于赋值运算符 1.2关系表达式 用关系运算符将两个表达式(可以是算术表达式或关系表达式,逻辑表达式,赋值表达式,字符

第六章习题11.输出以下图形

🌏个人博客:尹蓝锐的博客 希望文章能够给到初学的你一些启发~ 如果觉得文章对你有帮助的话,点赞 + 关注+ 收藏支持一下笔者吧~ 1、题目要求: 输出以下图形

【C++ Primer Plus习题】12.2

大家好,这里是国中之林! ❥前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。有兴趣的可以点点进去看看← 问题: 解答: main.cpp #include <iostream>#include "String.h"using namespace std;int main(){String s1(" and I am a

c++习题30-求10000以内N的阶乘

目录 一,题目  二,思路 三,代码    一,题目  描述 求10000以内n的阶乘。 输入描述 只有一行输入,整数n(0≤n≤10000)。 输出描述 一行,即n!的值。 用例输入 1  4 用例输出 1  24   二,思路 n    n!           0    1 1    1*1=1 2    1*2=2 3    2*3=6 4

智能工厂程序设计 之1 智能工厂都本俱的方面(Facet,Aspect和Respect)即智能依赖的基底Substrate 之1

Q1、昨天分别给出了三个智能工厂的 “面face”(里面inter-face,外面outer-face和表面surface) 以及每个“面face” 各自使用的“方”(StringProcessor,CaseFilter和ModeAdapter)  。今天我们将继续说说三个智能工厂的“方面” 。在展开之前先看一下三个单词:面向facing,取向oriented,朝向toword。理解这三个词 和

C语言程序与设计第四版课后习题 - 1~8章大合集

前言 本文章是一个大合集,按照课后习题的命名方式命名,方便寻找,只需要在目录上点相对应的题号即可在这里插入图片描述 第一章课后习题 1.1 编写一个C程序 题目概述: 请参照本章例题,编写一个C程序,输出一下信息: *****************************Very good!***************************** 代码实现: #define

C语言程序设计 笔记代码梳理 重制版

前言 本篇以笔记为主的C语言详解,全篇一共十章内容,会持续更新基础内容,争取做到更详细。多一句没有,少一句不行!  形而上学者谓之道,形而下学者谓之器 形而上学者谓之道,形而下学者谓之器 第1章 C语言的流程 1.C程序经历的六个阶段 编辑(Edit)预处理(Preprocess)编译(Compile)汇编(Assemble)链接(Link)执行(Execute)  2.

【C++ Primer Plus习题】12.1

大家好,这里是国中之林! ❥前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。有兴趣的可以点点进去看看← 问题: 解答: main.cpp #include <iostream>#include "Cow.h"using namespace std;int main(){Cow c1;Cow c2("老母牛", "喝奶"