C语言程序设计第五版谭浩强课后答案

最下方有所有答案的总目录
1.什么是文件型指针?通过文件指针访问文件有什么好处?

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

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

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

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

#include <stdio.h>
#include <stdlib.h>

int main( void ) {
FILE *fp = NULL;
char c;
int i;

if ( (fp=fopen("test", "w")) == NULL ) {
printf("open file test error!\n");
exit(EXIT_FAILURE);
}

while ( (c=getchar()) != EOF && c != '!' ) {
if ( c>='a' && c<='z' )
c = c-'a' + 'A';
fputc(c, fp);
}

fclose(fp);
}

结果:

输入 : 123我的AbcABC!
test文件的内容 : 123我的ABCABC

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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void swap(char *s, int i, int j) {
char t = s[i];
s[i] = s[j];
s[j] = t;
}

void select_sort(char *str) {
int i, j;
int len = strlen(str);
for (i=0; i<len; i++) {
int min = i;
for (j=i+1; j<len; j++) {
if ( str[j] < str[min] )
min = j;
}
swap(str, min, i);
}
}

int main( void ) {
FILE *fa, *fb, *fc;
char buf[1024] = {0};

fa = fopen("A", "r");
fb = fopen("B", "r");
fc = fopen("C", "w");

fgets(buf, 1024, fa);
int len = strlen(buf);
fgets(buf+len, 1024-len, fb);
select_sort(buf);
fputs(buf, fc);

fclose(fa);
fclose(fb);
fclose(fc);
}

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

#include <stdio.h>
#include <stdlib.h>

struct student {
int num;
char name[32];
int score[3];
float avg;
};

int main( viod ) {
int i;
struct student stu[5];
FILE *fp = NULL;

for (i=0; i<5; i++) {
printf("num name score1 score2 score3:\n");
scanf("%d %s %d %d %d", &stu[i].num, &stu[i].name,
&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
stu[i].avg = (stu[i].score[0]+stu[i].score[1]+stu[i].score[2])/3.0;
}

if ( (fp=fopen("stud", "wb")) == NULL ) {
printf("open file stud for write error\n");
return 1;
}

if ( fwrite(stu, sizeof(stu), 1, fp) != 1 ) {
printf("write error\n");
return 1;
}
fclose(fp);
}

测试程序查看输入文件的容:

1 zhagnsan 10 20 30 20.000000
2 lisi 3 6 9 6.000000
3 wangwu 1 2 3 2.000000
4 zhaoliu 90 80 10 60.000000
5 sunqi 90 1 1 30.000000

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

#include <stdio.h>
#include <stdlib.h>

struct student {
int num;
char name[32];
int score[3];
float avg;
};

void sort(struct student stu[], int len) {
int i, j;
struct student tmp;
for (i=0; i<len; i++) {
int min = i;
for (j=i+1; j<len; j++) {
if ( stu[j].avg > stu[min].avg )
min = j;
}
tmp = stu[min];
stu[min] = stu[i];
stu[i] = tmp;
}
}

int main( viod ) {
int i;
struct student stu[5];
FILE *fp = NULL;
if ( (fp=fopen("stud", "rb")) == NULL ) {
printf("open file stud for read error\n");
return 1;
}

if ( fread(stu, sizeof(stu), 1, fp) != 1 ) {
printf("write error\n");
return 1;
}
fclose(fp);

sort(stu, 5);

FILE *fw = fopen("stu_sort", "wb");
fwrite(stu, sizeof(stu), 1, fw);
fclose(fw);
}

测试程序,查看文件内容,确实排过序:

4 zhaoliu 90 80 10 60.000000
5 sunqi 90 1 1 30.000000
1 zhagnsan 10 20 30 20.000000
2 lisi 3 6 9 6.000000
3 wangwu 1 2 3 2.000000

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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct student {
int num;
char name[32];
int score[3];
float avg;
};

void sort(struct student stu[], int len) {
int i, j;
struct student tmp;
for (i=0; i<len; i++) {
int min = i;
for (j=i+1; j<len; j++) {
if ( stu[j].avg > stu[min].avg )
min = j;
}
if ( min != i ) {
tmp = stu[min];
stu[min] = stu[i];
stu[i] = tmp;
}
}
}
int main( viod ) {
int i;
struct student stu[5];
FILE *fp = NULL;
if ( (fp=fopen("stu_sort", "rb")) == NULL ) {
printf("open file stud for read error\n");
return 1;
}

if ( fread(stu, sizeof(stu), 1, fp) != 1 ) {
printf("write error\n");
return 1;
}
fclose(fp);

struct student new_stu[6];
memcpy(new_stu, stu, sizeof(stu));
printf("num name score0 score1 score2:\n");
scanf("%d %s %d %d %d", &new_stu[5].num, &new_stu[5].name, &new_stu[5].score[0],
&new_stu[5].score[1], &new_stu[5].score[2]);
new_stu[5].avg = (new_stu[5].score[0]+new_stu[5].score[1]+new_stu[5].score[2])/3.0;
sort(new_stu, 6);

FILE *fw = fopen("tmp_sort", "wb");
fwrite(new_stu, sizeof(new_stu), 1, fw);
fclose(fw);
}

查看tmp_sort文件,确实插入和排序了:

4 zhaoliu 90 80 10 60.000000
5 sunqi 90 1 1 30.000000
1 zhagnsan 10 20 30 20.000000
8 hehe 12 3 4 6.333333
2 lisi 3 6 9 6.000000
3 wangwu 1 2 3 2.000000

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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct student {
int num;
char name[32];
int score[3];
float avg;
};

int main( viod ) {
int i;
struct student stu[6];
FILE *fp = NULL;
if ( (fp=fopen("tmp_sort", "rb")) == NULL ) {
printf("open file stud for read error\n");
return 1;
}

if ( fread(stu, sizeof(stu), 1, fp) != 1 ) {
printf("write error\n");
return 1;
}
fclose(fp);

FILE *fw = fopen("stu_sort", "wb");
fwrite(stu, sizeof(stu), 1, fw);
fclose(fw);
}

查看原本的stu_sort文件:

4 zhaoliu 90 80 10 60.000000
5 sunqi 90 1 1 30.000000
1 zhagnsan 10 20 30 20.000000
8 hehe 12 3 4 6.333333
2 lisi 3 6 9 6.000000
3 wangwu 1 2 3 2.000000

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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct employee {
int num; // 编号
char name[32];
char sex[4];
int age;
char addr[60];
int salary;
char health[10]; // 健康状况
char class[10]; // 文化程度
};

struct emp {
char name[32];
int salary;
};

int main( void ) {
int i;
FILE *fp1, *fp2;
struct emp emp_arr[5];
struct employee employee_arr[5];

fp1=fopen("employee", "rb");
fread(employee_arr, sizeof(employee_arr), 1, fp1);
fclose(fp1);

for (i=0; i<5; i++) {
strcpy(emp_arr[i].name, employee_arr[i].name);
emp_arr[i].salary = employee_arr[i].salary;
}

fp2=fopen("emp", "wb");
fwrite(emp_arr, sizeof(emp_arr), 1, fp2);
fclose(fp2);
}

查看emp文件的内容如下:

abc 1800 
def 2000
hehe 3000
haha 2800
ggg 2500

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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct emp {
char name[32];
int salary;
};

int main( void ) {
int i;
FILE *fp;
char name[32];
struct emp emp_arr[5];

fp=fopen("emp", "rb");
fread(emp_arr, sizeof(emp_arr), 1, fp);
fclose(fp);

printf("name:");
scanf("%s", &name);
fp=fopen("emp", "wb");
for (i=0; i<5; i++) {
if ( strcmp(emp_arr[i].name, name) == 0 ) {
continue;
}
fwrite(&emp_arr[i], sizeof(emp_arr[i]), 1, fp);
}
fclose(fp);
}

删除ggg后的源文件内容:

abc 1800 
def 2000
hehe 3000
haha 2800

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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main( void ) {
int i;
FILE *fp = fopen("tmp.txt", "w");
char buf[1024] = {};

while ( fgets(buf, 1024, stdin) != NULL ) {
fputs(buf, fp);
memset(buf, 0x00, sizeof(buf));
}
fclose(fp);

fp = fopen("tmp.txt", "r");
while ( !feof(fp) ) {
memset(buf, 0x00, sizeof(buf));
fgets(buf, 1024, fp);
for (i=0; buf[i] != '\0'; i++) {
if ( buf[i]>='a' && buf[i]<='z' )
printf("%c", buf[i]-32);
else
printf("%c", buf[i]);
}
}
fclose(fp);
}

执行结果:

输入:
this is maomaochong
litao love IDFD
1243
输出:
THIS IS MAOMAOCHONG
LITAO LOVE IDFD
1243

​C语言程序设计第五版谭浩强课后习题答案 第一章​​​​C语言程序设计第五版谭浩强课后习题答案 第二章​​​​C语言程序设计第五版谭浩强课后习题答案 第三章​​​​C语言程序设计第五版谭浩强课后习题答案 第四章​​​​C语言程序设计第五版谭浩强课后习题答案 第五章​​​​C语言程序设计第五版谭浩强课后习题答案 第六章​​​​C语言程序设计第五版谭浩强课后习题答案 第七章​​​​C语言程序设计第五版谭浩强课后习题答案 第八章​​​​C语言程序设计第五版谭浩强课后习题答案 第九章​​​​C语言程序设计第五版谭浩强课后习题答案 第十章​