收藏 分销(赏)

第9章--文件练习题答案-华中社《C语言程序设计上机指导与练习》.doc

上传人:精**** 文档编号:3158276 上传时间:2024-06-21 格式:DOC 页数:15 大小:75.50KB
下载 相关 举报
第9章--文件练习题答案-华中社《C语言程序设计上机指导与练习》.doc_第1页
第1页 / 共15页
第9章--文件练习题答案-华中社《C语言程序设计上机指导与练习》.doc_第2页
第2页 / 共15页
点击查看更多>>
资源描述
第9章 文件 一、选择题 1. C 2. B 3. 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 file\n"); 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("文件中数据的个数为%d\n",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 data1\n"); exit(0); } if((out=fopen("data3","w"))==NULL) { printf("cannot open data3\n"); exit(0); } while(!feof(in1)) fputc(fgetc(in1),out); if((in2=fopen("data2","r"))==NULL) { printf("cannot open data2\n"); 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,infile[10],outfile[10]; printf("请输入原文件名:\n"); scanf("%s",infile); printf("请输入转换后文件名:\n"); scanf("%s",outfile); if ((in=fopen(infile,"r"))==NULL) { printf("cannot open infile\n"); exit(0); } if ((out=fopen(outfile,"w"))==NULL) { printf("cannot open outfile\n"); 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,infile[10],array[100]; int i=0; printf("请输入文件名:\n"); scanf("%s",infile); if ((in=fopen(infile,"r"))==NULL) { printf("cannot open outfile\n"); exit(0); } while(!feof(in)) { array[i]=fgetc(in); i++; } array[i]='\0'; printf("倒序打印为:\n"); for(i=strlen(array)-1;i>=0;i--) printf("%c",array[i]); fclose(in); } 6. 参考程序如下。 #include<stdlib.h> #include<stdio.h> #define N 10 struct student { char name[9]; int num; int age; char sex; }stu[N]; void main() { int i; char s[10]; 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(&stu[i],sizeof(struct student),1,fp); while(s==stu[i].name) printf("%s,%d,%d,%d\n",stu[i].name,stu[i].num,stu[i].age,stu[i].sex); } fclose(fp); } 7. 参考程序如下。 #include "stdio.h" main() { FILE *fp; int i,j,n; char c[160],t,ch; if((fp=fopen("A","r"))==NULL) { printf("file A cannot be opened\n"); exit(0); } printf("\n A contents are :\n"); for(i=0;(ch=fgetc(fp))!=EOF;i++) { c[i]=ch;putchar(c[i]); } fclose(fp); if((fp=fopen("B","r"))==NULL) { printf("file B cannot be opened\n"); exit(0); } printf("\n B contents are :\n"); for(i=0;(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("\n C file is:\n"); fp=fopen("C","w"); for(i=0;i<n;i++) { putc(c[i],fp); putchar(c[i]); } fclose(fp); } 8. 参考程序如下。 #include<stdio.h> #include<stdlib.h> #define N 30 void main() { FILE *fp; char c,file[N]; scanf("%s",file); if((fp=fopen(file,"w"))==NULL) { printf("Can not open file\n"); 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 name[10]; int age; char speciality[20]; }; FILE *fp; main() { struct student st; fp=fopen("student.dat","rb"); if(fp==NULL) printf("file not found\n"); else { while(!feof(fp)) { fread(&st,sizeof(struct student),1,fp); if(st.num>=970101&&st.num<=970135) printf("%ld,%s,%d,%s\n",st.num,st.name,st.age,st.speciality); } } } 10. 参考程序如下。 #include <stdio.h> #include <stdlib.h> struct clerk { char num[6]; char name[8]; char sex[2]; int age; double salary; }clk[10],clkread[10]; void main() { int i,sum; FILE *fp; /* 输入 */ for (i=0;i<10;i++ ) { printf("\n请输入职工%d的数据:\n",i+1); printf("职工号:"); scanf("%s",clk[i].num); printf("职工姓名:"); scanf("%s",clk[i].name); printf("性别:"); scanf("%s",clk[i].sex); printf("年龄:"); scanf("%d",&clk[i].age); printf("工资:"); scanf("%d",&clk[i].salary); } /*将数据写入文件*/ fp=fopen("worker.dat","w"); for (i=0;i<10;i++ ) if (fwrite(&clk[i],sizeof(struct clerk),1,fp)!=1) printf ("file write error\n"); 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 (&clkread[i],sizeof(struct clerk),1,fp)!=0;i++) { printf("\n%8s",clkread[i].num); printf("%8s",clkread[i].name); printf("%8s",clkread[i].sex); printf("%8d",clkread[i].age); printf("%8d",clkread[i].salary); } fclose(fp); } 11. 参考程序如下。 #include<stdio.h> struct student { char num[6]; char name[8]; int score[3]; float avr; }stu[5]; void main() { int i,j,sum; FILE *fp; /* 输入 */ for (i=0;i<5 ;i++ ) { printf("\n请输入学生%d的成绩:\n",i+1); printf("学号:"); scanf("%s",stu[i].num); printf("姓名:"); scanf("%s",stu[i].name); sum=0; for (j=0;j<3;j++ ) { printf("成绩 %d:",j+1); scanf("%d",&stu[i].score[j]); sum+=stu[i].score[j]; } stu[i].avr=(float)(sum/3.0); } /*将数据写入文件*/ fp=fopen("student","wb"); for (i=0;i<5;i++ ) if (fwrite(&stu[i],sizeof(struct student),1,fp)!=1) printf ("file write error\n"); fclose(fp); } 12. 参考程序如下。 #include<stdio.h> #include <stdlib.h> #define N 10 struct student { char num[6]; char name[8]; int score[3]; float avr; }st[N],change; void main() { FILE *fp; int i,j,n; /*读文件*/ if ((fp=fopen("student","r"))==NULL) { printf("文件打不开"); exit(0); } printf("\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].avr); } fclose(fp); /*排序*/ for(i=0;i<n;i++) for (j=i+1;j<n;j++) if(st[i].avr<st[j].avr) { change=st[i]; st[i]=st[j]; st[j]=change; } /*输出*/ printf("\n排序后:"); fp=fopen("student_sort","wb"); 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].avr); } fclose(fp); } 13. 参考程序如下。 #include<stdio.h> int main() { int i,flag; char str[80],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;str[i]!='\0';i++) if((str[i]>='a')&&(str[i]<='z')) str[i]-=32; printf("\n%s\n",str); } fclose(fp); return 0; }
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传
相似文档                                   自信AI助手自信AI助手

当前位置:首页 > 教育专区 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服