C程序设计(第5版)谭浩强习题解答
第10章 对文件的输入输出
1.什么是文件型指针?通过文件指针访问文件有什么好处?
缓冲文件系统中,关键的概念是“文件类型指针”,简称“文件指针”。每个被使用的文件都在内存中开辟一个相应的文件信息区,用来存放文件的有关信息(如文件的名字、文件状态及文件当前位置等)。这些信息是保存在一个结构体变量中的。该结构体类型是由系统声明的,取名为FILE。
通过文件指针访问文件的好处是:可以随机访问文件,有效表示数据结构,动态分配内存,方便使用字符串,有效使用数组。
2.对文件的打开与关闭的含义是什么?为什么要打开和关闭文件?
答:”打开“是指为文件建立相应的信息区(用来存放有关文件的信息)和文件缓冲区(用来暂时存放输人输出的数据)。
”关闭“是指撤销文件信息区和文件缓冲区,使文件指针变量不再指向该文件,显然就无法进行对文件的读写了。
3.从键盘输入一个字符串,将其中的小写字母全部转换成大写字母,然后输出到一个磁盘文件test中保存,输入的字符串以“!”结束。
#include
#include
#include
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
#include
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
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
#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
#include
#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
#include
#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
#include
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
#include
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
#include
#include
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
#include
#include
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;
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 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
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;
}