1、第9章 文件 一、选择题 1. C 2. B 3. A 二、填空题 1. 二进制文件 2. 顺序 随机 流 流式 3. hello, 4. picursound 三、简答题 1. 什么是文件?文件分为哪些类型? 答:文件一般是指存储在计算机外部介质上的数据的集合。文件分为两种类型:ASCII文件和二进制文件。 2. 什么是缓冲文件系统?什么是非缓冲文件系统? 答:缓冲文件系统是指系统自动地在内存区为每个正在使用的文件开辟一个缓冲区。从内存向磁盘输出数据时,先将数据送入输出文件缓冲区中,当输出文件缓冲区写满时,再一起写到磁盘上。如果从磁盘向内存读入数
2、据,则一次从磁盘文件将一批数据读取到输入缓冲区,然后再从缓冲区逐个地将数据送到程序数据区。 非缓冲文件系统是指系统不自动开辟确定大小的缓冲区,而由程序为每个文件设定缓冲区。 3. 如何理解文件的打开和关闭?为什么要打开和关闭文件? 答:打开文件,是指建立文件与对应缓冲区之间的联系,即建立与文件对应的信息表。关闭文件,是指撤销与操作文件相关的流。即通过关闭操作,通知系统释放相应的文件信息区。这样,原来的文件指针变量不再指向该文件,此后,也就不可能通过此指针来访问该文件。 要对文件操作必须打开文件,即建立与文件对应的信息表。而如果不关闭文件将会丢失数据。 4. 简述文件指针的概念。如何通
3、过文件指针访问一个文件?
答:文件指针指的是指向文件结构体的指针变量。要通过文件指针访问一个文件,必须先定义这个文件指针,然后打开文件即可。例如:
FILE *fp;
fp=fopen("d:\\exam1.txt","r")
就可以通过文件指针访问d:\exam1.txt文件。
四、编程题
1. 参考程序如下。
#include
4、"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); } pr
5、intf("文件中的数据为:\n");
rewind(fp);
while(!feof(fp))
{
fscanf(fp,"%f,",&data);
printf("%f ",data);
num++;
}
num--;
printf("文件中数据的个数为%d\n",num);
fclose(fp);
}
2. 参考程序如下。
#include
6、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(!fe
7、of(in2))
fputc(fgetc(in2),out);
fclose(in1);
fclose(in2);
fclose(out);
return 0;
}
3. 参考程序如下。
#include
8、以#结束\n");
ch=getchar();
while(ch!='#')
{
fputc(ch,fp);
ch=getchar();
}
fclose(fp);
}
4. 参考程序如下。
#include
9、anf("%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); }
10、
fclose(in);
fclose(out);
}
5. 参考程序如下。
#include
11、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
12、um;
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 13、nt),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");
14、
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 15、i++)
{ c[i]=ch;
putchar(c[i]);
}
fclose(fp);
n=i;
for(i=0;i 16、8. 参考程序如下。
#include 17、r(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)
18、
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 19、h>
#include 20、 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 21、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 22、)
{
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 23、 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);
sc 24、anf("%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 25、include 26、t[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 27、
st[i]=st[j];
st[j]=change;
}
/*输出*/
printf("\n排序后:");
fp=fopen("student_sort","wb");
for(i=0;i 28、avr);
}
fclose(fp);
}
13. 参考程序如下。
#include
©2010-2025 宁波自信网络信息技术有限公司 版权所有
客服电话:4009-655-100 投诉/维权电话:18658249818