第十章习题及答案复习进程.docx
《第十章习题及答案复习进程.docx》由会员分享,可在线阅读,更多相关《第十章习题及答案复习进程.docx(11页珍藏版)》请在冰豆网上搜索。
![第十章习题及答案复习进程.docx](https://file1.bdocx.com/fileroot1/2023-1/11/f262fb88-e566-4edc-8ec9-4931943f54c6/f262fb88-e566-4edc-8ec9-4931943f54c61.gif)
第十章习题及答案复习进程
第十章-习题及答案
第十章习题
一、选择题
1.以下叙述中正确的是________。
A)C语言中的文件是流式文件,因此只能顺序存取数据
B)打开一个已存在的文件并进行了写操作后,原有文件中的全部数据必定被覆盖
C)在一个程序中当对文件进行了写操作后,必须先关闭该文件然后再打开,才能读到第1个数据
D)当对文件的读(写)操作完成之后,必须将它关闭,否则可能导致数据丢失
2.当已存在一个abc.txt文件时,执行函数fopen(“abc.txt”,“r++”)的功能是。
A)打开abc.txt文件,清除原有的内容
B)打开abc.txt文件,只能写入新的内容
C)
打开abc.txt文件,只能读取原有内容
D)打开abc.txt文件,可以读取和写入新的内容
3.若fp是指向某文件的指针,且已读到此文件末尾,则库函数feof(fp)的返回值是。
A)EOFB)0C)非零值D)NULL
4.以下程序企图把从终端输入的字符输出到名为abc.txt的文件中,直到从终端读入字符#号时结束输入和输出操作,但程序有错。
#include
main()
{FILE*fout;charch;
fout=fopen('abc.txt','w');
ch=fgetc(stdin);
while(ch!
='#')
{fputc(ch,fout);
ch=fgetc(stdin);
fclose(fout);
}
出错的原因是。
A)函数fopen调用形式有误B)输入文件没有关闭C)函数fgetc调用形式有误D)文件指针stdin没有定义
5.有以下程序
#include
main()
{FILE*pf;
char*s1=〞China〞,*s2=〞Beijing〞;
pf=fopen(〞abc.dat〞,〞wb+〞);
fwrite(s2,7,1,pf);
rewind(pf);
fwrite(s1,5,1,pf);
fclose(pf);
}
以下程序执行后abc.dat文件的内容是
A)ChinaB)Chinang
C)ChinaBeijingD)BeijingChina
6.有以下程序
#include main() {FILE*fp;inti,a[6]={1,2,3,4,5,6}; fp=fopen(“d3.dat”,”w+b”); fwrite(a,sizeof(int),6,fp); fseek(fp,sizeof(int)*3,SEEK_SET); fread(a,sizeof(int),3,fp);fclose(fp); for(i=0;i<6;i++)printf(“%d,”,a[i]); }
程序运行后的输出结果是()
A)4,5,6,4,5,6, B)1,2,3,4,5,6, C)4,5,6,1,2,3, D)6,5,4,3,2,1,
7.有以下程序
#include
main()
{FILE*fp;inta[10]={1,2,3},i,n;
fp=fopen(“dl.dat”,”w”);
for(i=0;i<3;i++)fprintf(fp,”%d”,a[i]);
fprintf(fp,”\n”);
fclose(fp);
fp=fopen(“dl.dat”,”r”);
fscanf(fp,”%d”,&n);
fclose(fp);
printf(“%d\n”,n);
}
程序的运行结果是
A)12300B)123C)1D)321
8.设有以下结构体类型:
structst
{charname[8];
intnum;
floats[4];
}student[20];
并且结构体数组student中的元素都已经有值,若要将这些元素写到fp所指向的磁盘文件中,以下不正确的形式是( )。
A)fwrite(student,sizeof(structst),20,fp);
B)fwrite(student,20*sizeof(structst),1,fp);
C)fwrite(student,10*sizeof(structst),10,fp);
D)for(i=0;i<20;i++)
fwrite(student+i,sizeof(structst),1,fp);
二、填空题
1.C语言中根据数据的组织形式,把文件分为和两种。
2.在C语言中,文件的存取是以为单位的,这种文件被称作文件。
3.以下程序的功能是:
从键盘上输入一个字符串,把该字符串中的小写字母转换为大写字母,输出到文件test.txt中,然后从该文件读出字符串并显示出来。
请填空。
#include
main()
{FILE*fp;
charstr[100];inti=0;
if((fp=fopen("text.txt",____
(1)_______))==NULL)
{printf("can'topenthisfile.\n");exit(0);}
printf("inputastring:
\n");gets(str);
while(str[i])
{if(str[i]>='a'&&str[i]<='z')
str[i]=____
(2)_______;
fputc(str[i],fp);
i++;
}
fclose(fp);
fp=fopen("test.txt",____(3)__________);
fgets(str,100,fp);
printf("%s\n",str);
fclose(fp);}
4.下面程序用变量count统计文件中字符的个数。
请填空。
#include
main()
{FILE*fp;longcount=0;
if((fp=fopen(“letter.dat”,
(1)))==NULL)
{printf(“cannotopenfile\n”);exit(0);}
while(!
feof(fp)){
(2);(3);}
printf(“count=%ld\n”,count);fclose(fp);}
5.以下程序的功能是将文件file1.c的内容输出到屏幕上并复制到文件file2.c中。
请填空。
#include
main()
{FILE
(1);
fp1=fopen(“file1.c”,”r”);
fp2=fopen(“file2.c”,”w”);
while(!
feof(fp1))putchar(getc(fp1));
(2)
while(!
feof(fp1))putc((3));
fclose(fp1);fclose(fp2);}
6.以下程序段打开文件后,先利用fseek函数将文件位置指针定位在文件末尾,然后调用ftell函数返回当前文件位置指针的具体位置,从而确定文件长度,请填空。
FILE*myf;longf1;
myf=fopen(“test.t”,“rb”);
;
f1=ftell(myf);
fclose(myf);
printf(“%d\n”,f1);
三、编程题
1.编写一个程序,建立一个abc文本文件,向其中写入“thisisatest”字符串,然后显示该文件的内容。
2.有5个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号、姓名、三门课成绩),计算出平均成绩,将原有数据和计算出的平均分数存放在磁盘文件stud中。
3.将上题stud文件中的学生数据按平均分进行排序处理,并将已排序的学生数据存入一个新文件stu-sort中。
4.将上题以排序的学生成绩文件进行插入处理。
插入一个学生的3门课成绩,程序先计算新插入学生的平均成绩,然后将它按平均成绩高低顺序插入,插入后建立一个新文件。
第十章习题及答案
一、选择题
1-8DDAABABC
二、填空题
1.文本文件、二进制文件
2.字节流式
3.
(1)"w"或"w+"或"wt"或"w+t"或"""wt+"
(2)str[i]-32
(3)"r"或"r+"或"r+t"或"rt+"
4.
(1)"r"
(2)fgetc(fp)(3)count++
5.
(1)*fp1,*fp2
(2)rewind(fp1);(3)getc(fp1),fp2
6.fseek(myf,0,SEEK_END)
三、编程题
1.编写一个程序,建立一个abc文本文件,向其中写入“thisisatest”字符串,然后显示该文件的内容。
#include
#include
#include
main()
{FILE*fp;
charmsg[]="thisisatest";
charbuf[20];
if((fp=fopen("abc","w+"))==NULL)
{printf("不能建立abc文件\n");
exit(0);
}
fwrite(msg,strlen(msg)+1,1,fp);
fseek(fp,SEEK_SET,0);
fread(buf,strlen(msg)+1,1,fp);
printf("%s\n",buf);
fclose(fp);
}
2.有5个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号、姓名、三门课成绩),计算出平均成绩,将原有数据和计算出的平均分数存放在磁盘文件stud中。
#include
structstudent
{charnum[10];
charname[8];
intscore[3];
floatave;
}stu[5];
main()
{inti,j,sum;
FILE*fp;
for(i=0;i<5;i++)
{printf("\ninputscoreofstudent%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(structstudent),1,fp)!
=1)
printf("Filewriteerror\n");
fclose(fp);
fp=fopen("stud","r");
for(i=0;i<5;i++)
{fread(&stu[i],sizeof(structstudent),1,fp);
printf("%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);
}
}
3.将上题stud文件中的学生数据按平均分进行排序处理,并将已排序的学生数据存入一个新文件stu-sort中。
#include
#include
#defineN10
structstudent
{charnum[10];
charname[8];
intscore[3];
floatave;
}st[N],temp;
main()
{
FILE*fp;
inti,j,n;
if((fp=fopen("stud","r"))==NULL)
{printf("cannotopenthefile");
exit(0);
}
printf("\nfile'stud':
");
for(i=0;fread(&st[i],sizeof(structstudent),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.f",st[i].ave);
fclose(fp);
n=i;
}
for(i=0;ifor(j=i+1;jif(st[i].ave{temp=st[i];
st[i]=st[j];
st[j]=temp;
}
printf("\nnow:
");
fp=fopen("stu-sort","w");
for(i=0;i{fwrite(&st[i],sizeof(structstudent),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);
fclose(fp);
}
}
4.将上题以排序的学生成绩文件进行插入处理。
插入一个学生的3门课成绩,程序先计算新插入学生的平均成绩,然后将它按平均成绩高低顺序插入,插入后建立一个新文件。
#include
#include
structstudent
{charnum[10];
charname[8];
intscore[3];
floatave;
}st[10],s;
main()
{
FILE*fp,*fp1;
inti,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("cannotopenfile.");
exit(0);
}
printf("originaldata:
\n");
for(i=0;fread(&st[i],sizeof(structstudent),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&&tprintf("\nnow:
\n");
fp1=fopen("sort1.dat","w");
for(i=0;i{fwrite(&st[i],sizeof(structstudent),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(structstudent),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{fwrite(&st[i],sizeof(structstudent),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);
fclose(fp);
fclose(fp1);
}
}