c程序设计 第13章 文件 答案解析.docx
- 文档编号:28640061
- 上传时间:2023-07-19
- 格式:DOCX
- 页数:43
- 大小:23.83KB
c程序设计 第13章 文件 答案解析.docx
《c程序设计 第13章 文件 答案解析.docx》由会员分享,可在线阅读,更多相关《c程序设计 第13章 文件 答案解析.docx(43页珍藏版)》请在冰豆网上搜索。
c程序设计第13章文件答案解析
第13章
例13.1程序为:
#include
#include
voidmain()
{
FILE*fp;
charch,filename[10];
scanf("%s",filename);
if((fp=fopen(filename,"w"))==NULL)
{
printf("cannotopenfile\n");
exit(0);
}
ch=getchar();
ch=getchar();
while(ch!
='#')
{
fputc(ch,fp);putchar(ch);
ch=getchar();
}
putchar(10);
fclose(fp);
}
例13.2程序为:
#include
#include
voidmain()
{
FILE*in,*out;
charch,infile[10],outfile[10];
printf("Entertheinfilename:
\n");
scanf("%s",infile);
printf("Entertheinfilename:
\n");
scanf("%s",outfile);
if((in=fopen(infile,"r"))==NULL)
{
printf("cannotopenfile\n");
exit(0);
}
if((out=fopen(outfile,"w"))==NULL)
{
printf("cannotopenfile\n");
exit(0);
}
while(!
feof(in))fputc(fgetc(in),out);
fclose(in);
fclose(out);
}
注:
在该程序的运行中,每复制一次就会产生一个乱码置于最后,并依次叠加。
这不是个优秀的程序。
13.5.1rewind函数:
#include
voidmain()
{
FILE*fp1,*fp2;
fp1=fopen("file1.c","r");
fp2=fopen("file2.c","w");
while(!
feof(fp1))putchar(getc(fp1));
rewind(fp1);
while(!
feof(fp1))putc(getc(fp1),fp2);
fclose(fp1);fclose(fp2);
}
例13.5
#include
#include
structstudent_type
{
charname[10];
intnum;
intage;
charsex;
}stud[10];
voidmain()
{
inti;
FILE*fp;
if((fp=fopen("file22.c","rb"))==NULL)
{
printf("cannotopenfile\n");
exit(0);
}
for(i=0;i<10;i+=2)
{
fseek(fp,i*sizeof(structstudent_type),0);
fread(&stud[i],sizeof(structstudent_type),1,fp);
printf("%s%d%d%c\n",stud[i].name,stud[i].num,stud[i].age,stud[i].sex);
}
fclose(fp);
}
注:
这个程序是对的,但常用于处理二进制文件,因为文本文件要发生字符转换,计算位置时常发生混乱。
习题
13.1对c文件操作有些什么特点?
什么是缓冲文件系统?
什么是非缓冲文件系统?
这两者的缓冲区有什么区别?
答:
c语言把文件看作是一个字符的序列级,即由一个一个字符的数据顺序组成。
根据数据的组织形式,可以分为:
ASCII文件(文本文件)和二进制文件。
一个c文件是一个字节流或二进制流(流式文件)。
它允许对文件存取一个字符,这就增加了处理的灵活性。
缓冲文件系统:
是指系统自动在内存中为每一个正在使用的文件开辟一个缓冲区,如果从磁盘向内存读入数据,则一次从磁盘文件将一批数据输入到内存缓冲区,然后再从缓冲区逐个地将数据送到程序数据区中去。
非缓冲文件系统:
是指系统不自动开辟确定大小的缓冲区,而有程序为每个文件设定缓冲区。
用缓冲文件系统进行的输入输出又称高级磁盘输入输出,用非缓冲文件系统进行的输入输出又称为低级输出输入系统。
ANSIC标准不采用非缓冲文件系统,而只采用缓冲文件系统,即既用缓冲文件系统处理文本文件,也用它处理二进制文件,也就是将缓冲文件系统扩充为可以处理二进制文件。
13.2什么是文件型指针?
通过文件型指针访问文件有什么好处?
答:
缓冲文件系统中,关键的概念是“文件指针”。
每个被使用的文件都在内存中开辟一个区,用来存放文件的有关信息(如文件的名字,文件的状态及文件当前位置等)。
这些信息是保存在一个结构体变量中的。
该结构体类型是由系统定义的,取名为FILE。
这和一般使用指针变量的好处是一样的。
13.3对文件的打开和关闭的含义是什么?
为什么要打开和关闭文件?
答:
打开的含义:
带回指向a1文件的指针赋给fp,这样fp就和文件a1联系上了。
或者说,fp指向a1文件。
在使用完一个文件后应该关闭它,以防止它再被误用。
“关闭”就是文件指针变量不指向该文件,也就是文件指针变量与文件“脱钩”,此后不能再通过该指针对原来与其联系的文件进行读写操作,除非再次打开,使该指针变量重新指向该文件。
13.4程序为:
#include
#include
voidmain()
{
FILE*fp;
charch,filename[10];
printf("pleaseinputthenameofthefile:
\n");
scanf("%s",filename);
if((fp=fopen(filename,"w"))==NULL)
{
printf("cannotopenfile\n");
exit(0);
}
ch=getchar();
printf("pleaseastring:
\n");
ch=getchar();
if('a'<=ch&&ch<='z')
ch=ch-32;
while(ch!
='!
')
{
fputc(ch,fp);putchar(ch);
ch=getchar();
if('a'<=ch&&ch<='z')
ch=ch-32;
}
putchar(10);
fclose(fp);
}
13.5程序为:
#include
#include
#include
voidmain()
{
FILE*fp;
chara[100];
if((fp=fopen("test.c","r"))==NULL)
{
printf("文件打开错误");
exit(0);
}
inti=0;
a[i]=fgetc(fp);
while(a[i]!
=EOF)
{
putchar(a[i]);
i++;
a[i]=fgetc(fp);
}
putchar('\n');
fclose(fp);
if((fp=fopen("test1.c","r"))==NULL)
{
printf("文件打开错误");
exit(0);
}
a[i]=fgetc(fp);
while(a[i]!
=EOF)
{
putchar(a[i]);
i++;
a[i]=fgetc(fp);
}
a[i]='\0';
putchar('\n');
fclose(fp);
intn=i;
intk;
chart;
for(i=0;i { k=i; for(intj=i+1;j { if(a[k]>a[j])k=j; } if(k! =i) { t=a[i]; a[i]=a[k]; a[k]=t; } } if((fp=fopen("test2.c","w"))==NULL) { printf("文件打开错误"); exit(0); } i=0; while(i { fputc(a[i],fp); putchar(a[i]); i++; } fclose(fp); putchar('\n'); } 注: 这个程序将处理数组的排序问题和文件读取的问题结合起来了。 从而解决了这个题目。 13.6程序为: (这个程序不难,自己编的,对其中文件类型和它和结构体数组之间信息的转换有了较深的认识) #include #defineSIZE5 structstudent_type { intnum; charname[10]; floatscore1; floatscore2; floatscore3; floatavg; }stud[SIZE]; voidsave() { FILE*fp; inti; if((fp=fopen("stu_list.c","wb"))==NULL) { printf("cannotopenfile\n"); return; } for(i=0;i if(fwrite(&stud[i],sizeof(student_type),1,fp)! =1) printf("filewriteerror"); fclose(fp); } voidmain() { FILE*fp; inti; for(i=0;i { scanf("%d%s%f%f%f",&stud[i].num,stud[i].name,&stud[i].score1,&stud[i].score2,&stud[i].score3); stud[i].avg=(stud[i].score1+stud[i].score2+stud[i].score3)/3; } save(); fp=fopen("stu_list.c","rb"); for(i=0;i { fread(&stud[i],sizeof(student_type),1,fp); printf("%-4d%-10s%-10.2f%-10.2f%-10.2f%-10.2f\n",stud[i].num,stud[i].name,stud[i].score1,stud[i].score2,stud[i].score3,stud[i].avg); } } 13.7程序为: #include #defineSIZE5 structstudent_type { intnum; charname[10]; floatscore1; floatscore2; floatscore3; floatavg; }stud[SIZE]; voidsave() { FILE*fp; inti; if((fp=fopen("stu_sort.c","wb"))==NULL) { printf("cannotopenfile\n"); return; } for(i=0;i if(fwrite(&stud[i],sizeof(student_type),1,fp)! =1) printf("filewriteerror"); fclose(fp); } voidmain() { FILE*fp; inti,j; structstudent_typetemp; fp=fopen("stu_list.c","rb"); for(i=0;i { fread(&stud[i],sizeof(student_type),1,fp); } for(i=0;i for(j=i+1;j { if(stud[i].avg>stud[j].avg) { temp=stud[i]; stud[i]=stud[j]; stud[j]=temp; } } save(); fp=fopen("stu_sort.c","rb"); for(i=0;i { fread(&stud[i],sizeof(student_type),1,fp); printf("%-4d%-10s%-10.2f%-10.2f%-10.2f%-10.2f\n",stud[i].num,stud[i].name,stud[i].score1,stud[i].score2,stud[i].score3,stud[i].avg); } } 13.9程序为: (这个题的程序要建立在前面那些程序的基础之上) #include #defineSIZE5 structstudent_type { intnum; charname[10]; floatscore1; floatscore2; floatscore3; floatavg; }stud[SIZE+1]; voidsave() { FILE*fp; inti; if((fp=fopen("stu_sort.c","wb"))==NULL) { printf("cannotopenfile\n"); return; } for(i=0;i if(fwrite(&stud[i],sizeof(student_type),1,fp)! =1) printf("filewriteerror"); fclose(fp); } voidmain() { FILE*fp; inti,j; structstudent_typetemp,another; scanf("%d%s%f%f%f",&another.num,another.name,&another.score1,&another.score2,&another.score3); another.avg=(another.score1+another.score2+another.score3)/3; fp=fopen("stu_list.c","ab"); fwrite(&another,sizeof(student_type),1,fp); fclose(fp); fp=fopen("stu_list.c","rb"); for(i=0;i { fread(&stud[i],sizeof(student_type),1,fp); } for(i=0;i for(j=i+1;j { if(stud[i].avg>stud[j].avg) { temp=stud[i]; stud[i]=stud[j]; stud[j]=temp; } } fclose(fp); save(); fp=fopen("stu_sort.c","rb"); for(i=0;i { fread(&stud[i],sizeof(student_type),1,fp); printf("%-4d%-10s%-10.2f%-10.2f%-10.2f%-10.2f\n",stud[i].num,stud[i].name,stud[i].score1,stud[i].score2,stud[i].score3,stud[i].avg); } fclose(fp); } 13.8程序为: (由上题易得该程序,这里建立的新文件的名字为: students.c) #include #defineSIZE5 structstudent_type { intnum; charname[10]; floatscore1; floatscore2; floatscore3; floatavg; }stud[SIZE+1]; voidsave() { FILE*fp; inti; if((fp=fopen("students.c","wb"))==NULL) { printf("cannotopenfile\n"); return; } for(i=0;i if(fwrite(&stud[i],sizeof(student_type),1,fp)! =1) printf("filewriteerror"); fclose(fp); } voidmain() { FILE*fp; inti,j; structstudent_typetemp,another; scanf("%d%s%f%f%f",&another.num,another.name,&another.score1,&another.score2,&another.score3); another.avg=(another.score1+another.score2+another.score3)/3; fp=fopen("stu_list.c","ab"); fwrite(&another,sizeof(student_type),1,fp); fclose(fp); fp=fopen("stu_list.c","rb"); for(i=0;i { fread(&stud[i],sizeof(student_type),1,fp); } for(i=0;i for(j=i+1;j { if(stud[i].avg>stud[j].avg) { temp=stud[i]; stud[i]=stud[j]; stud[j]=temp; } } fclose(fp); save(); fp=fopen("students.c","rb"); for(i=0;i { fread(&stud[i],sizeof(student_type),1,fp); printf("%-4d%-10s%-10.2f%-10.2f%-10.2f%-10.2f\n",stud[i].num,stud[i].name,stud[i].score1,stud[i].score2,stud[i].score3,stud[i].avg); } fclose(fp); } 13.10程序为: #include #defineSIZE1 structemployee_type {charname[10]; intnum; intage; charsex[5]; charhealth[10]; intsalary; charadress[10]; charedu[10]; }emp[SIZE]; structwage_type { charname[10]; intsalary; }emp1[SIZE]; voidsave() { FILE*fp; inti; if((fp=fopen("employee_list.c","wb"))==NULL) { printf("cannotopenfile\n"); return; } for(i=0;i if(fwrite(&emp[i],sizeof(employee_type),1,fp)! =1) printf("filewriteerror"); fclose(fp); } voidsave1() { FILE*fp; inti; if((fp=fopen("employee_list1.c","wb"))==NULL) { printf("cannotopenfile\n"); return; } for(i=0;i if(fwrite(&emp1[i],sizeof(wage_type),1,fp)! =1) printf("filewriteerror"); fclose(fp); } voidmain() { FILE*fp; inti; for(i=0;i { scanf("%s%d%d%d%s%s%s%s",emp[i].name,&emp[i].num,&emp[i].age,&emp[i].salary,emp[i].adress,emp[i].edu,emp[i].health,emp[i].sex); printf("%s%d%d%d%s%s%s%s\n",emp[i].name,emp[i].num,emp[i].age,emp[i].salary,emp[i].adress,emp[i].edu,emp[i].health,emp[i].sex); } save(); fp=fopen("employee_list.c","rb"); for(i=0;i { fread(&emp[i],sizeof(employee_type),1,fp); for(intj=0;j<10;j++) emp1[i].name[j]=emp[i].name[j]; emp1
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- c程序设计 第13章 文件 答案解析 程序设计 13 答案 解析