C语言程序设计第2版李学刚课堂实践答案 4512页.docx
《C语言程序设计第2版李学刚课堂实践答案 4512页.docx》由会员分享,可在线阅读,更多相关《C语言程序设计第2版李学刚课堂实践答案 4512页.docx(14页珍藏版)》请在冰豆网上搜索。
![C语言程序设计第2版李学刚课堂实践答案 4512页.docx](https://file1.bdocx.com/fileroot1/2022-12/30/e8634348-c38a-464d-bef5-8323cf2fbfa1/e8634348-c38a-464d-bef5-8323cf2fbfa11.gif)
C语言程序设计第2版李学刚课堂实践答案4512页
同步训练9-2答案
一、单项选择题
1.已知函数的调用形式:
fread(buf,size,count,fp);其中buf代表的是(C)。
A.一个整形变量,代表要读入的数据项总数
B.一个文件指针,指向要读的文件
C.一个指针,指向要读入数据的存放地址
D.一个存储区,存放要读的数据项
2.若有以下定义和说明:
#include
structstd
{
charnum[6];
charname[8];
floatmark[4];
}a[30];
FILE*fp;
设文件中以二进制形式存有10个班的学生数据,且已正确打开,文件位置指针定位于文件开头。
若要从文件中读出30个学生的数据放入a数组中,以下不能实现此功能的语句是(D)。
A.for(i=0;i<30;i++)
fread(&a[i],sizeof(structstd),1L,fp);
B.for(i=0;i<30;i++)
fread(a+i,sizeof(structstd),1L,fp);
C.fread(a,sizeof(structstd),30L,fp);
D.for(i=0;i<30;i++)
fread(a[i],sizeof(structstd),1L,fp);
3.fscanf函数的正确调用形式是(D)。
A.fscanf(格式字符串,输出表列)
B.fscanf(格式字符串,输出表列,fp)
C.fscanf(格式字符串,文件指针,输出表列)
D.fscanf(文件指针,格式字符串,输出表列)
4.fwrite函数的一般调用形式是(D)。
A.fwrite(buffer,count,size,fp)B.fwrite(fp,size,count,buffer)
C.fwrite(fp,count,size,buffer)D.fwrite(buffer,size,count,fp)
5.若调用fputc函数成功输出字符,则其返回值是(D)。
A.EOFB.1C.0D.输出的字符
6.设有以下结构体类型:
structst
{
charname[8];
intnum;
floats[4];
}student[50];
并且结构体数组student中的元素都已有值,若要将这些元素写到硬盘文件中,以下不正确的形式是(C)。
A.fwrite(student,sizeof(structst),50,fp);
B.fwrite(student,50*sizeof(structst),1,fp);
C.fwrite(student,25*sizeof(structst),25,fp);
D.for(i=0;i<50;i++)
fwrite(student+i,sizeof(structst),1,fp);
7.阅读以下程序及对程序功能的描述,其中正确的描述是(C)。
#include
intmain()
{
FILE*in,*out;
charinfile[10],outfile[10];
intc;
printf("Entertheinfilename:
\n");
scanf("%s",infile);
printf("Entertheoutfilename:
\n");
scanf("%s",outfile);
if((in=fopen(infile,"r"))==NULL)
{printf("cannotopeninfile\n");exit(0);}
if((out=fopen(outfile,"w"))==NULL
{printf("cannotopenoutfile\n");exit(0);}
while((ch=fgetc(in))!
=EOF)
fputc(ch,out);
fclose(in);
fclose(out);
}
A.程序完成将磁盘文件的信息在屏幕上显示的功能
B.程序完成将两个磁盘文件合二为一的功能
C.程序完成将一个磁盘文件复制到另一个磁盘文件中
D.程序完成将两个磁盘文件合并且在屏幕上输出
二、知识填空题
1.函数调用语句:
fgets(buf,n,fp);从fp指向的文件中读入n-1个字符放到buf字符数组中,函数返回值为buf的首地址。
2.设有以下结构体类型:
structst
{
charname[8];
intnum;
floats[4];
}student[50];
并且结构数组student中的元素都已有值,若要将这些元素写到硬盘fp文件中,请将以下fwrite语句补充完整:
fwrite(student,50*sizeof(st),1,fp);
3.feof(fp)函数用来判断文件是否结束,如果遇到文件结束,函数值为1,否则为0。
4.用fopen(文件名,使用方式)打开文件,当使用方式为“r”时,只
能读入数据,不能进行写操作;当使用方式为“w”时,只能写入数据,不能进行读数据操作。
5.当调用函数fread从磁盘文件中读数据时,若函数返回值为10,则表明读入了10个字符;若函数的返回值为0,则是遇到了文件结束符;若函数返回值为-1,则意味着读文件出错。
三、程序填空题
1.以下程序由终端输入一个文件名,然后把从终端键盘输入的字符依次存放到该文件中,用#作为结束输入的标志。
请填空。
#include
intmain()
{
FILE*fp;
charch,fname[10];
printf("lnputthenameoffile\n");
gets(fname);
if((fp=fopen(fname,"w"))==NULL)
{printf("Cannotopen\n");exit(0);}
printf("Enterdata\n");
while((ch=getchar())!
='#')fputc(ch,fp);
fclose(fp);
return0;
}
2.下面程序用变量count统计文件中字符的个数,请填入适当内容。
#include
intmain()
{
FILE*fp;longcount=0;
if((fp=fopen("letter.dat","r"))==NULL)
{printf("cannotopenfile\n");exit(0);}
while(fgetc(fp)!
=EOF)count++;
frintf("count=%ld\n",count);
fclose(fp);
return0;
}
3.下面程序由终端键盘输入字符,存放到文件中,用!
结束输入,请填入适当的内容。
#include
#include
intmain()
{
FILE*fp;
charch,fname[10];
printf("inputnameoffile\n");
gets(fname);
if((fp=fopen(fname,"w"))==NULL)
{
printf("cannotopen\n");
exit(0);
}
printf("enterdata:
\n");
while(ch=getchar()!
='!
')
fputc(ch,fp);
fclose(fp);
return0;
}
4.下面程序从一个二进制文件中读入结构体数据,并把结构体数据显示在终端屏幕上。
请填入适当内容。
#include
structrec
{
intnum;
floattotal;
};
intmain()
{
FILE*f;
if((f=fopen("bin.dat","rb"))==NULL);
{printf("cannotopen\n");exit(0);}
reout(f);
fclose(f);
return0;
}
voidreout(FILE*f)
{
structrec,r;
fread(&r,sizeof(rec),1,f);
while(!
feof(f))
{
printf("%df,%f\n",r.num,r.total);
fread(&r,sizeof(rec),1,f);
}
}
5.下面程序把从终端读入的10个整数以二进制方式写到一个名为bi.dat的新文件中,请填空。
#include
FILE*fp;
intmain()
{
inti,j;
if((fp=fopen("bi.dat","wb"))==NULL)
exit(0);
for(i=0;i<10;i++)
{
scanf("%d",&j);
fwrite(&j,sizeof(int),1,fp);
}
fclose(fp);
return0;
}
6.以下程序的功能是将C语言源程序文件exam.c中用反斜杠与星号括起来的非嵌套注释删除,然后存入文件exam.out中。
请填入适当内容。
#include
voiddelcomm(FILE*fp1,FILE*fp2)
{
intc,i=0;
while((c=fgetc(fp1))!
=EOF)
if(c=='\n')
fprintf(fp2,"\n");
else
switch(i)
{
case0:
if(c=='/')i=1;
elsefprintf(fp2,"%c",c);
break;
case1:
if(c=='*')i=2;
else
{fprintf(fp2,"%c",c);i=0;}
break;
case2:
if(c=='*')i=3;
break;
case3:
i=(c=='/')?
0:
2;
break;
}
}
intmain()
{
FILE*fp1,*fp2;
fp1=fopen("exam.c","r");
fp2=fopen("exam.out","w");
delcomm(fp1,fp2);
fcloseall();
return0;
}
7.以下程序的功能是将文件file1.c的内容输出到屏幕上并复制到文件file2.c中,请填入适当内容。
#include
intmain()
{
FILE*fp1,*fp2;
charch;
fp1=fopen("file1.c","r");
fp2=fopen("file2.c","w");
while((ch=fgetc(fp1))!
=EOF)
{putchar(ch);fputc(fp2,ch)}
fclose(fp1);
fclose(fp2);
return0;
}
8.以下程序的功能是用“追加”的形式打开gg.txt察看文件指针的位置。
其中ftell(*FILE)返回long型的文件指针位置。
程序执行前gg.txt内容为:
sample.请填入适当内容。
#include
intmain()
{
FILE*fp;
longposition;
fp=fopen("gg.txt","a");
position=ftell(fp);
printf("position=%ld\n",position);
fprintf(fp,"sampledata\n");
position=ftell(fp);
printf("position=%ld\n",position);
fclose(fp);
return0;
}
四、程序设计题
1.编程实现以下功能:
从键盘输入一个字符串,将文件中的大写字母全部转换成小写字母,输出到磁盘文件letter.txt中保存,以字符#表示输入结束。
参考代码:
#include
#include
voidfun(FILE*fp)
{
charf;
printf("请输入一个字符串(#号结束输入):
\n");
f=getchar();
while(f!
='#')
{
if(f>='A'&&f<='Z')
f=f+32;
fputc(f,fp);
f=getchar();
}
}
intmain()
{
FILE*fp;
if((fp=fopen("letter.txt","w"))==NULL)
{
printf("不能打开文件\n");
exit(0);
}
fun(fp);
fclose(fp);
return0;
}
2.有一文件num.txt中存有形如-1,0,1,2,5,3,0,-1,2,4,5,5,6,7,0,的数据,编程统计出文件中正整数的个数。
参考代码:
#include
#include
#defineN15
voidwrite();
intread();
intmain()
{
intt;
write();
t=read();
printf("正整数有%d个。
\n",t);
return0 ;
}
intread()
{
FILE*fp;
intt,n=0;
if((fp=fopen("num.txt","r"))==NULL)
{
printf("cannotopenfile\n");
exit(0);
}
fscanf(fp,"%d%,",&t);
while(!
feof(fp))
{
if(t>0)n++;
fscanf(fp,"%d%,",&t);
}
fclose(fp);
returnn;
}
voidwrite()
{
FILE*fp;
inti,a[N]={-1,0,1,2,5,3,0,-1,2,4,5,5,6,7,0};
if((fp=fopen("num.txt","w"))==NULL)
{
printf("cannotopenfile\n");
exit(0);
}
for(i=0;ifprintf(fp,"%d%c",a[i],',');
fclose(fp);
}
3.在文件worker.dat中存放着工人的基本信息,用以下结构体来描述:
structworker
{
intnum;//工号
charname[10];//姓名
intage;//年龄
charsex;//性别
chardept[20];//部门
};
请编程查找工号为1001的人工信息并输出在屏幕上。
存放工人的信息文件worker.dat需自己编写程序形成,也可以参考下列程序实现:
voidwrite()
{
structworkerw;
FILE*fp;
inti;
if((fp=fopen("worker.dat","wb"))==NULL)
{
printf("cannotopenstu.csvfile\n");
exit(0);
}
for(i=0;i<5;i++)
{
printf("\n输入第%d个工人\n工号:
",i+1);
scanf("%d",&w.num);
getchar();
printf("姓名:
");
gets(w.name);
printf("年龄:
");
scanf("%d",&w.age);
getchar();
printf("性别:
");
scanf("%c",&w.sex);
getchar();
printf("部门:
");
gets(w.dept);
fwrite(&w,sizeof(structworker),1,fp);
}
fclose(fp);
}
参考代码:
#include"stdio.h"
#include"stdlib.h"
structworker
{
intnum;//工号
charname[10];//姓名
intage;//年龄
charsex;//性别
chardept[20];//部门
};
voidwrite();
voidread();
intmain()
{
write();
read();
return0;
}
voidread()
{
FILE*fp;
structworkerwrk;
if((fp=fopen("worker.dat","rb"))==NULL)
{printf("cannotopenfile\n");exit(0);}
fread(&wrk,sizeof(structworker),1,fp);
while(!
feof(fp))
{
if(wrk.num==1001)
printf("num:
%dname:
%sage:
%dsex:
%cdept:
%s\n",
wrk.num,wrk.name,wrk.age,wrk.sex,wrk.dept);
fread(&wrk,sizeof(structworker),1,fp);
}
fclose(fp);
}