《C语言程序设计》(谭浩强第五版) 第10章 对文件的输入输出习题答案

你也可以上程序咖(https://meta.chengxuka.com),打开大学幕题板块,不但有答案,讲解,还可以在线答题。

WX20220412-135428@2x

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

答:

缓冲文件系统中,关键的概念是“文件类型指针”,简称“文件指针”。每个被使用的文件都在内存中开辟一个相应的文件信息区,用来存放文件的有关信息(如文件的名字、文件状态及文件当前位置等)。这些信息是保存在一个结构体变量中的。该结构体类型是由系统声明的,取名为FILE。所以,在C语言上,文件型指针就是指FILE类型的指针,它指向一个文件类型的结构,结构里包含着该文件的各种属性。

通过文件指针访问文件的好处是:可以随机访问文件,有效表示数据结构,动态分配内存,方便使用字符串,有效使用数组。

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

答:

打开文件,是建立文件的各种相关信息,并使文件类型指针指向该文件,以便进行其它操作。而关闭文件操作可强制把缓冲区中的数据写入磁盘文件,保证文件的完整性,同时还将释放文件缓冲区单元和FILE结构,使文件类型指针与具体文件脱钩。

要对一个文件进行操作,必须先将其打开,读写完毕后还要将其关闭,以防止不正常的操作。

”打开“是指为文件建立相应的信息区(用来存放有关文件的信息)和文件缓冲区(用来暂时存放输出的数据)。

”关闭“是指撤销文件信息区和文件缓冲区,使文件指针变量不再指向该文件,显然就无法进行对文件的读写了。

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

解:

答案代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
	FILE *fp;
	char str[100];
	int i = 0;
	if ((fp = fopen("test.txt", "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("test.txt", "r");
	fgets(str, strlen(str) + 1, fp);
	printf("%s\n", str);
	fclose(fp);
	return 0;
}

运行结果:

WX20220330-164423@2x

可以发现多了一个文件:test.txt,我们打开它:

WX20220330-164654@2x

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

解:先用第 3 题的程序分别建立两个文件 A 和 B ,其中内容分别是 “I LOVE CHINA” 和 “I LOVE BEIJING” 。
在程序中先分别将 A,B 文件的内容读出放到数组 c 中,再对数组 c 排序。最后将数组内容写到 C 文件中。流程图如图10.1所示。

WX20220330-164807@2x

程序如下∶

#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;
}

首先要创建文件a1和b1,里面分别存入题目要求的文本内容。然后再运行程序。

运行结果:

WX20220330-172832@2x

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

解:

解法一:N-S图如图10.2所示。

WX20220330-164933@2x

答案代码:

#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;
}

运行结果:

WX20220330-180658@2x

说明:在程序的第 1 个 for 循环中,有两个 printf 函数语句用来提示用户输入数据,即"printf(“input score of student %d∶\n”,i+1);" 和 “printf(“score %d;”,j+1);” ,其中用 “i十1” 和 “j十1” 而不是用 i 和 j 的用意是使显示提示时,序号从 1 起,即学生 1 和成绩 1(而不是学生 0 和成绩 0 ),以符合们的习惯,但在内存中数组元素下标仍从 0 算起。

程序最后 5 行用来检查文件 stud 中的内容是否正确,从结果来看,是正确的。请注意:用 fwrite 函数向文件输出数据时不是按 ASCII 码方式输出的,而是按内存中存储数据的方式输出的(例如一个整数占 2 个或 4 个字节,一个实数占 4 个字节),因此不能用 DOS 的 type 命令输出该文件中的数据。

解法二:也可以用下面的程序来实现:

#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(); //调用 save 函数,向文件 stu.dat 输出数据

	fp1 = fopen("stu.dat", "rb"); //用只读方式打开 stu. dat文件
	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);
}

运行结果∶

WX20220330-183511@2x

本程序用 save 函数将数据写到磁盘文件上,再从文件读回,然后用 printf 函数输出,从运行结果可以看到文件中的数据是正确的。

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

解:

解法一:N-S图如图10.3所示。

WX20220331-100102@2x

答案代码:

#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;
}

运行结果:

WX20220331-101908@2x

解法二:与第5题解法二相应,可以接着使用下面的程序来实现本题要求。

#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);
}

运行结果:

WX20220331-111425@2x

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

解:N-S图如图10.4所示。

WX20220331-112239@2x

答案代码:

#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;
}

运行结果:

WX20220331-141057@2x

为节省篇幅,本题和第8题不再给出第6题"解法二"的程序,请读者自已编写程序。

题目8:将第 7 题结果仍存入原有的 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;
}

运行结果:

WX20220331-142235@2x

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

解:N-S图如图10.5所示。

WX20220331-141310@2x

答案代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct employee
{
	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("employee", "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 employee), 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\n**************************************************");
	fclose(fp1);
	fclose(fp2);
	return 0;
}

运行结果:

WX20220331-144045@2x

说明:数据文件 employee 是事先建立好的,其中已有职工数据,而 emp_salary 文件则是由程序建立的。

建立 employee 文件的程序如下:

#include <stdio.h>
#include <stdlib.h>
struct employee
{
	char num[6];
	char name[10];
	char sex[2];
	int age;
	char addr[20];
	int salary;
	char health[8];
	char class[10];
} em[10];

int main()
{
	FILE *fp;
	int i;
	printf("input NO.,name,sex,age,addr,salary,health,class\n");
	for (i = 0; i < 4; i++)
		scanf("%s %s %s %d %s %d %s %s", em[i].num, em[i].name, em[i].sex, &em[i].age, em[i].addr, &em[i].salary, em[i].health, em[i].class);

	//将数据写入文件
	if ((fp = fopen("employee", "w")) == NULL)
	{
		printf("can not open file.");
		exit(0);
	}
	for (i = 0; i < 4; i++)
		if (fwrite(&em[i], sizeof(struct employee), 1, fp) != 1)
			printf("error\n");
	fclose(fp);
	return 0;
}

在运行此程序时从键盘输入4个职工的数据,程序将它们写入 employee 文件。在运行前面一个程序时从 employee 文件中读出数据并输出到屏幕,然后建立一个简明文件,同时在屏幕上输出。

WX20220331-144253@2x

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

解:N-S图如图10.6所示。

WX20220331-144329@2x

答案代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct employee
{
	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 employee), 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[i].name, emp[+1].name);
				emp[j].salary = emp[j + 1].salary;
			}
			flag = 0;
		}
	}
	if (!flag)
		n = n - 1;
	else
		printf("\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 employee), 1, fp);
	fclose(fp);
	fp = fopen("emp_salary", "r");
	for (i = 0; fread(&emp[i], sizeof(struct employee), 1, fp) != 0; i++)
		printf("\n%8s %7d", emp[i].name, emp[i].salary);
	printf("\n");
	fclose(fp);
	return 0;
}

运行结果:

WX20220331-145238@2x

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

解:N-S图如图 10.7所示。

WX20220331-145425@2x

答案代码:

#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\n", 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;
}

运行结果:

WX20220331-150953@2x

此程序运行结果是正确的,但是如果输入的字符串中包含了空格,就会发生一些问题,例如输入:

inut string:

i am a student.

得到的结果是:

I

AM

A

STUDENT.

把一行分成几行输出。这是因为用fscanf 函数从文件读入字符串时,把空格作为一个字符串的结束标志,因此把该行作为 4 个字符串来处理,分别输出在 4 行上。请读者考虑怎样解决这个问题。

WX20220331-151029@2x

  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值