第9章--文件练习题答案-华中社《C语言程序设计上机指导与练习》.doc
第9章 文件一、选择题1. C2. B3. A二、填空题1. 二进制文件2. 顺序 随机 流 流式3. hello,4. picursound三、简答题1. 什么是文件?文件分为哪些类型?答:文件一般是指存储在计算机外部介质上的数据的集合。文件分为两种类型:ASCII文件和二进制文件。2. 什么是缓冲文件系统?什么是非缓冲文件系统?答:缓冲文件系统是指系统自动地在内存区为每个正在使用的文件开辟一个缓冲区。从内存向磁盘输出数据时,先将数据送入输出文件缓冲区中,当输出文件缓冲区写满时,再一起写到磁盘上。如果从磁盘向内存读入数据,则一次从磁盘文件将一批数据读取到输入缓冲区,然后再从缓冲区逐个地将数据送到程序数据区。非缓冲文件系统是指系统不自动开辟确定大小的缓冲区,而由程序为每个文件设定缓冲区。3. 如何理解文件的打开和关闭?为什么要打开和关闭文件?答:打开文件,是指建立文件与对应缓冲区之间的联系,即建立与文件对应的信息表。关闭文件,是指撤销与操作文件相关的流。即通过关闭操作,通知系统释放相应的文件信息区。这样,原来的文件指针变量不再指向该文件,此后,也就不可能通过此指针来访问该文件。要对文件操作必须打开文件,即建立与文件对应的信息表。而如果不关闭文件将会丢失数据。4. 简述文件指针的概念。如何通过文件指针访问一个文件?答:文件指针指的是指向文件结构体的指针变量。要通过文件指针访问一个文件,必须先定义这个文件指针,然后打开文件即可。例如:FILE *fp;fp=fopen("d:exam1.txt","r")就可以通过文件指针访问d:exam1.txt文件。四、编程题1. 参考程序如下。#include<stdio.h> #include<stdlib.h>void main(void)float data;int i,num=0;char ch;FILE *fp;if(fp=fopen("8_5.txt","w+")=NULL)printf("cannot open filen");exit(1); printf("请输入10个浮点数:n");for(i=1;i<=10;i+)scanf("%f",&data);fprintf(fp,"%f,",data);printf("请输入若干个浮点数,以#结束:n");while(ch=getchar()!='#')scanf("%f",&data);fprintf(fp,"%f,",data); printf("文件中的数据为:n");rewind(fp);while(!feof(fp)fscanf(fp,"%f,",&data);printf("%f ",data);num+;num-;printf("文件中数据的个数为%dn",num);fclose(fp);2. 参考程序如下。#include<stdio.h> #include<stdlib.h> int main ()FILE *in1,*in2,*out;if(in1=fopen("data1","r")=NULL)printf("cannot open data1n");exit(0);if(out=fopen("data3","w")=NULL)printf("cannot open data3n");exit(0);while(!feof(in1)fputc(fgetc(in1),out);if(in2=fopen("data2","r")=NULL)printf("cannot open data2n");exit(0);while(!feof(in2)fputc(fgetc(in2),out);fclose(in1);fclose(in2);fclose(out); return 0;3. 参考程序如下。#include<stdio.h>#include<stdlib.h>void main()FILE *fp;char ch;if(fp=fopen("file3.txt","w")=NULL)printf("cannot open file n");exit(0);printf("请输入一个字符串,以#结束n");ch=getchar();while(ch!='#')fputc(ch,fp);ch=getchar();fclose(fp);4. 参考程序如下。#include <stdio.h>#include <stdlib.h>void main()FILE *in,*out;char ch,infile10,outfile10;printf("请输入原文件名:n");scanf("%s",infile);printf("请输入转换后文件名:n");scanf("%s",outfile);if (in=fopen(infile,"r")=NULL)printf("cannot open infilen");exit(0);if (out=fopen(outfile,"w")=NULL)printf("cannot open outfilen");exit(0);while(!feof(in)ch=fgetc(in);if(ch>='a'&&ch<='z') ch=ch-32;fputc(ch,out);fclose(in);fclose(out);5. 参考程序如下。#include<stdio.h>#include<stdlib.h>#include<string.h>void main()FILE *in;char ch,infile10,array100;int i=0;printf("请输入文件名:n");scanf("%s",infile);if (in=fopen(infile,"r")=NULL)printf("cannot open outfilen");exit(0);while(!feof(in)arrayi=fgetc(in);i+;arrayi='0' printf("倒序打印为:n");for(i=strlen(array)-1;i>=0;i-)printf("%c",arrayi);fclose(in);6. 参考程序如下。#include<stdlib.h>#include<stdio.h>#define N 10struct student char name9; int num; int age; char sex;stuN;void main() int i; char s10; FILE *fp; if(fp=fopen("stu","rb")=NULL) printf("Can not open file!n"); exit(0); gets(s); for(i=0;i<N;i+) fseek(fp,i*sizeof(struct student),0); fread(&stui,sizeof(struct student),1,fp); while(s=stui.name) printf("%s,%d,%d,%dn",stui.name,stui.num,stui.age,stui.sex); fclose(fp);7. 参考程序如下。#include "stdio.h"main() FILE *fp; int i,j,n; char c160,t,ch; if(fp=fopen("A","r")=NULL) printf("file A cannot be openedn"); exit(0); printf("n A contents are :n"); for(i=0;(ch=fgetc(fp)!=EOF;i+) ci=ch;putchar(ci); fclose(fp); if(fp=fopen("B","r")=NULL) printf("file B cannot be openedn"); exit(0); printf("n B contents are :n"); for(i=0;(ch=fgetc(fp)!=EOF;i+) ci=ch; putchar(ci); fclose(fp); n=i; for(i=0;i<n;i+) for(j=i+1;j<n;j+) if(ci>cj) t=ci;ci=cj;cj=t; printf("n C file is:n"); fp=fopen("C","w"); for(i=0;i<n;i+) putc(ci,fp); putchar(ci); fclose(fp);8. 参考程序如下。#include<stdio.h>#include<stdlib.h>#define N 30void main() FILE *fp; char c,fileN; scanf("%s",file); if(fp=fopen(file,"w")=NULL) printf("Can not open filen"); exit(0); c=getchar(); c=getchar(); while(c!='n') fputc(c,fp); putchar(c); c=getchar(); putchar(N); fclose(fp);9. 参考程序如下。#include "stdio.h"struct student long int num; char name10; int age; char speciality20; ; FILE *fp; main() struct student st; fp=fopen("student.dat","rb"); if(fp=NULL) printf("file not foundn"); else while(!feof(fp) fread(&st,sizeof(struct student),1,fp); if(st.num>=970101&&st.num<=970135) printf("%ld,%s,%d,%sn",st.num,st.name,st.age,st.speciality); 10. 参考程序如下。#include <stdio.h>#include <stdlib.h>struct clerkchar num6;char name8;char sex2;int age;double salary;clk10,clkread10;void main()int i,sum;FILE *fp;/* 输入 */for (i=0;i<10;i+ )printf("n请输入职工%d的数据:n",i+1);printf("职工号:");scanf("%s",clki.num);printf("职工姓名:");scanf("%s",clki.name);printf("性别:");scanf("%s",clki.sex);printf("年龄:");scanf("%d",&clki.age);printf("工资:");scanf("%d",&clki.salary);/*将数据写入文件*/fp=fopen("worker.dat","w");for (i=0;i<10;i+ )if (fwrite(&clki,sizeof(struct clerk),1,fp)!=1)printf ("file write errorn");fclose(fp);/*读文件*/if (fp=fopen("worker.dat","r")=NULL)printf("文件打不开");exit(0);printf("n文件内容是:n"); printf("%8s%8s%8s%8s%8s","职工号","职工姓名","性别","年龄","工资");for (i=0;fread (&clkreadi,sizeof(struct clerk),1,fp)!=0;i+)printf("n%8s",clkreadi.num);printf("%8s",clkreadi.name);printf("%8s",clkreadi.sex);printf("%8d",clkreadi.age);printf("%8d",clkreadi.salary);fclose(fp);11. 参考程序如下。#include<stdio.h>struct student char num6;char name8;int score3;float avr;stu5;void main()int i,j,sum;FILE *fp;/* 输入 */for (i=0;i<5 ;i+ )printf("n请输入学生%d的成绩:n",i+1);printf("学号:");scanf("%s",stui.num);printf("姓名:");scanf("%s",stui.name);sum=0;for (j=0;j<3;j+ )printf("成绩 %d:",j+1);scanf("%d",&stui.scorej);sum+=stui.scorej;stui.avr=(float)(sum/3.0);/*将数据写入文件*/fp=fopen("student","wb");for (i=0;i<5;i+ )if (fwrite(&stui,sizeof(struct student),1,fp)!=1)printf ("file write errorn");fclose(fp);12. 参考程序如下。#include<stdio.h>#include <stdlib.h>#define N 10struct studentchar num6;char name8;int score3;float avr;stN,change;void main() FILE *fp;int i,j,n;/*读文件*/if (fp=fopen("student","r")=NULL)printf("文件打不开");exit(0);printf("n文件内容是:");for(i=0;fread(&sti,sizeof(struct student),1,fp)!=0;i+)printf("n%8s%8s",sti.num,sti.name);for (j=0;j<3 ;j+ )printf ("%8d",sti.scorej);printf ("%10.2f",sti.avr);fclose(fp);/*排序*/for(i=0;i<n;i+)for (j=i+1;j<n;j+)if(sti.avr<stj.avr)change=sti;sti=stj;stj=change;/*输出*/printf("n排序后:");fp=fopen("student_sort","wb");for(i=0;i<n;i+)fwrite(&sti,sizeof(struct student),1,fp);printf("n%8s%8s",sti.num,sti.name);for(j=0;j<3;j+)printf("%8d",sti.scorej);printf("%10.2f",sti.avr);fclose(fp);13. 参考程序如下。#include<stdio.h> int main() int i,flag; char str80,c; FILE *fp; fp=fopen("letter.txt","w+"); for(flag=1;flag;) printf("n请输入字符串:n");gets(str);fprintf(fp,"%s",str);printf("是否继续输入?");if (c=getchar()='N')|(c='n')flag=0;getchar(); fseek(fp,0,0);while(fscanf(fp,"%s",str)!=EOF)for(i=0;stri!='0'i+)if(stri>='a')&&(stri<='z')stri-=32;printf("n%sn",str);fclose(fp); return 0;