第八章文件.docx
《第八章文件.docx》由会员分享,可在线阅读,更多相关《第八章文件.docx(28页珍藏版)》请在冰豆网上搜索。
第八章文件
第八章文件
第八章文件
一、单项选择题
1.C语言中,文件由(A)。
A.字符(字节)序列组成B.记录组成
C.数据行组成D.数据块组成
2.若文件型指针fp中指向某文件的末尾,则函数feof(fp)的返回值是(C)。
A.0B.-1C.非零值D.NULL
3.下列语句将输出(B)
#include
printf(“%d%d%d”,NULL,’\0’,EOF);
A.001B.00–1C.NULLEOFD.10EOF
4.下列语句中,将fp定义为文件型指针的是(B)。
A.FILEfp;B.FILE*fp;C.filefp;D.file*fp;
5.定义FILE*fp;则文件指针fp指向的是(D)。
A.文件在磁盘上的读写位置B.文件在缓冲区上的读写位置
C.整个磁盘文件D.文件类型结构体
6.缓冲文件系统的缓冲区位于(C)。
A.磁盘缓冲区中B.磁盘文件中
C.内存数据区中D.程序中
7.以“只读”方式打开文本文件a:
\aa.dat,下列语句中哪一个是正确的(D)。
A、fp=fopen(”a:
\aa.dat”,”ab”);B、fp=fopen(”a:
\aa.dat”,”a”);
C、fp=fopen(”a:
\aa.dat”,”wb”);D、fp=fopen(”a:
\aa.dat”,”r”);
8.以“追加”方式打开文本文件a:
\aa.dat,下列语句中哪一个是正确的(B)。
A、fp=fopen(”a:
\aa.dat”,”ab”);B、fp=fopen(”a:
\aa.dat”,”a”);
C、fp=fopen(”a:
\aa.dat”,”r+”);D、fp=fopen(”a:
\aa.dat”,”w”);
9.如果二进制文件a.dat已存在,现要求写入全新的数据,应以什么方式打开(A)。
A.“w”B.“wb”C.“w+”D.“wb+”
10.为读写建立一个新的文本文件a:
\aa.dat,下列语句中哪一个是正确的(B)。
A、fp=fopen(”a:
\aa.dat”,”ab”);B、fp=fopen(”a:
\aa.dat”,”w+”);
C、fp=fopen(”a:
\aa.dat”,”wb”);D、fp=fopen(”a:
\aa.dat”,”rb+”);
11.以读写方式打开一个已有的二进制文件filel,并且定义FILE*fp,下面fopen函数正确
的调用方式是(B)。
A.fp=fopen(“file1”,”r”)B.fp=fopen(“file1”,”rb+”)
C.fp=fopen(“file1”,”rb”)D.fp=fopen(“file1”,”wb+”)
12.标准库函数fputs(p1,p2)的功能是(C)。
A.从p1指向的文件中读一个字符串存入p2指向的内存
B.从p2指向的文件中读一个字符串存入p1指向的内存
C.从p1指向的内存中读一个字符串写到p2指向的文件中
D.从p2指向的内存中读一个字符串写到p1指向的文件中
二、填充题
1.C语言中调用fopen函数打开文件,调用fclose函数关闭文件。
2.fopen函数的返回值是包含文件缓冲区信息的FILE结构体地址。
3.文件操作的三大特征是文件保存在外存中,文件中数据有序,数据数量可以不定。
4.feof函数可用于文本文件和二进制文件,它用来判断即将读入的是
否为文件结束标志,若是,函数值为非零。
5.若ch为字符变量,fp为文本文件指针,从fp所指文件中读入一个字符时,可用的二
种不同的输入语句是ch=fgetc(fp)和fscanf(fp,”%c”,&ch)。
把一个字符输
出到fp所指文件中的二种不同的输出语句是fputc(ch,fp)和fprintf(fp,”%c”,ch)。
6.sp=fgets(str,n,fp);函数调用语句从fp指向的文件输入n-1个字符,并把它们放到
字符数组str中,sp得到str的地址。
而fputs函数的作用是向指定的文件输出
一个字符串,输出成功函数返回值为0,fprintf函数输出成功函数返回值为
实际输出的字符数。
7.“FILE*fp”的作用是定义了一个文件型指针变量,其中的“FILE”是在stdio.h
头文件中定义的。
8.在对文件进行操作的过程中,若要求文件指针的位置回到文件的开头,应当调用的函数
是rewind()函数。
9.下面的程序用来统计文件中字符的个数,请填空。
#include
voidmain()
{FILE*fp;
longnum=0;
if(fp=fopen(“fname.dat”,”r”)==NULL)
{printf(“Can’topenfile!
\n”);exit(0);}
while(!
feof(fp)){fgetc(fp);num++;}
printf(“num=%d\n”,num);
fclose(fp);
}
10.下面程序完成从磁盘文件stu..dat中读取N个学生的姓名、学号、成绩后在屏幕上显示
输出。
请将程序补充完整。
#include
#defineN100
structstudent
{charname[20];
intno;
intscore;
}stud[N];
voidmain()
{FILE*fp;
inti;
if((fp=fopen(“stu.dat”,”rb”))==NULL)
{printf(“Cannotopenfile!
\n”);exit(0);}
fread(stud,sizeof(structstudent),N,fp);
for(i=0;iprintf(“name:
%sNO:
%dscore:
%d\n”,stud[i].name,stud[i].no,stud[i].score);
fclose(fp)
}
11.开辟一个存储n个int数据的内存区、并将内存区首地址送入p(指向int类型的指
针变量)的语句为p=(int)malloc(sizeof(int)*n);。
12.释放由fp所指向的内存区的语句为free(fp);。
13.fp为文件位置指针,将fp移到离当前位置25个字节处的语句为fseek(fp,25L,1);。
14.输入若干个学生的姓名、学号、数学、英语、计算机成绩到文件中student.dat。
#include
voidmain()
{FILE*fp;charname[8],numb[8];intm,e,c,n,i;
scanf("%d",&n);fp=fopen(“student.dat”,”w”);
for(i=1;i<=n;i++){
scanf("%s%s%d%d%d",name,numb,&m,&e,&c);
fprintf(fp,"%s%s%d%d%d\n",name,numb,m,e,c);
}
fclose(fp);;
}
15.下列程序将文件a.dat、b.dat按原顺序(文件中每行一个数,按从小到大排列)
合并到文件c.dat中。
#include
voidmain()
{FILE*f1,*f2,*f3;intx,y;
if((f1=fopen("a.dat","r"))==NULL)
{printf(“Cannotopenfile!
\n”);exit(0);}
if((f2=fopen("b.dat","r"))==NULL)
{printf(“Cannotopenfile!
\n”);exit
(1);}
if((f3=fopen(“c.dat”,”w”))==NULL)
{printf(“Cannotopenfile!
\n”);exit
(2);}
fscanf(f1,"%d",&x);fscanf(f2,"%d",&y);
while(!
feof(f1)&&!
feof(f2))
if(xfprintf(f3,"%d\n",x);fscanf(f1,"%d",&x);
}
else{
fprintf(f3,"%d\n",y);fscanf(f2,"%d",&y);
}
if(feof(f1)){
fprintf(f3,”%d\n”,y);
while(!
feof(f2)){
fscanf(f2,"%d",&y);fprintf(f3,”%d\n”,y);
}
}
else{
fprintf(f3,“%d\n”,x);
while(!
feof(f1)){
fscanf(f1,”%d”,&x);;fprintf(f3,"%d\n",x);
}
}
fclose(f1);fclose(f2);fclose(f3);
}
三、程序分析题
1.读程序,指出程序所完成的功能。
#include
voidmain()
{intchl,ch2;
while((chl=getchar())!
=EOF)
if(ch1>=’a’&&chl<=’z’)
{ch2=chl-32;putchar(ch2);}
elseputchar(chl):
}
该程序的功能是:
从键盘读入一个字符,如果是小写字母,则变成大写输出到屏幕上,
否则原样输出。
当打入ctr1-z(即EOF)时,输入结束。
2.读程序,写功能。
#include
voidmain(intargc,char*argv[])
{FILE*f1,*f2;
charch;
if(argc<3)
{printf(“Parametermissing!
\n”);exit(0);}
if(((f1=fopen(argv[1],”r”))==NULL||(f2=fopen(argv[2],”w”))==NULL)
{printf(“Cannotopenfile!
\n”);exit(0);}
while(!
feof(f1))fputc(fgetc(f1),f2);
fclose(f1);fclose(f2);
}
该程序的功能是:
在用命令行的形式运行本程序时,必须在程序名后加二个参数,
这二个参数分别表示磁盘上的二个文件名。
程序运行后,能把第一个文件的内容拷贝
到第二个文件。
如果命令行缺少参数或文件打不开,程序终止运行并显示出错情况。
3.读程序,写功能。
#include
voidmain()
{FILE*fp:
intn=0;charch;
if((fp=fopen(“fname.txt”,”r”))==NULL)
{prlntf(“Cannotopenfile!
\n”);exit(0);}
while(!
feof(fp))
{ch=fgetc(fp);if(ch==’’)n++;}
printf(“b=%d\n”,n);
fclose(fp);
}
该程序的功能是:
统计文本文件fname.txt中的空格字符数。
4.读程序,写功能。
#include“stdio.h”
voidmain()
{FILE*f1,*f2;
intk;
if((f1=fopen(“c:
\tc\pl.c”,”r”))==NULL)
{printf(“Cannotopenfile!
\n”);
exit(0);
}
if((f2=fopen(“a:
\pl.c”,”w”))==NULL)
{printf(“Cannotopenfile!
\n”);
exit(0);
}
for(k=1;k<=1000;k++)
{if(!
feof(f1))break;
fputc(fgetc(f1),f2);
}
fclose(f1);fclose(f2);
}
该程序的功能是:
把C盘根目录下的tc目录中的文件p1.c复制到A盘的根目录下,
取同样的文件名p1.c。
如果C盘中的p1.c文件超过1000字节,则最多复制1000字节。
5.假定在当前盘当前目录下有2个文本文件,其名称和内容如下:
文件名a1.txta2.txt
内容121314#252627#
写出下列程序运行后的输出。
#include“stdio.h”
voidmain()
{FILE*fp;
voidfc(FILE*fp1);
if((fp=fopen(“a1.txt”,”r”))==NULL)
{printf(“Cannotopenfile!
\n”);exit
(1);}
else{fc(fp);fclose(fp);}
if((fp=fopen(“a2.txt”,”r”))==NULL)
{printf(“Cannotopenfile!
\n”);exit
(1);}
else{fc(fp);fclose(fp);}
}
voidfc(FILE*fp1)
{charc;
while((c=fgetc(fp1))!
=’#’)putchar(c);
}
该程序先打开a1.txt文件,然后调用函数fc(),把文件中#号字符以前的内容在屏幕
上现示。
再打开a2.txt文件,然后调用函数fc(),把文件中#号字符以前的内容在屏
幕上接着现示。
因此程序运行后输入121314252627
6.写出以下程序运行结果。
#include
voidmain()输出结果:
all=abcdefghijklmnop
{FILE*fp;inti;seek1ch=a
chars1[80],s[]="abcdefghijklmnop";seek2ch=l
if((fp=fopen("alf.dat","wb+"))==NULL)seek3ch3=m
{printf(“Cannotopenfile!
\n”);exit
(1);}
i=sizeof(s);
fwrite(s,i,1,fp);rewind(fp);fread(s1,i,l,fp);
printf("all=%s\n",s1);fseek(fp,0,0);
printf("seek1ch=%c\n",fgetc(fp));
fseek(fp,10,1);
printf("seek2ch=%c\n",fgetc(fp));
fseek(fp,1,1);printf("seek3ch=%c\n",fgetc(fp));
fclose(fp);
}
四、程序设计题.
1.编一个程序,从键盘输入200个字符,存入名为“f1.txt”的磁盘文件中。
#include“stdio.h”
voidmain()
{FILE*fp;
inti;
if((fp=fopen(“f1.txt”,”w”))==NULL)
{printf(“Cannotopenfile!
\n”);exit
(1);}
for(i=1;i<=200;i++)fputc(getchar(),fp);
fclose(fp);
}
2.设计一个复制文本文件的程序,源文件名和目标文件名随程序执行时输入。
#include
voidmain(intargc,char*argv[])
{FILE*f1,*f2;
charch;
if(argc<3)
{printf(“Error!
Useage:
program_namesource_file_nameobject_file_name\n”);
exit(0);}
if((f1=fopen(argv[1],”r”))==NULL)
{printf(“Cannotopensourcefile!
\n”);exit(0);}
if((f2=fopen(argv[2],”w”))==NULL)
{printf(“Cannotopenobjectfile!
\n”);exit
(1);}
while(!
feof(f1))fputc(fgect(f1),f2);
fclose(f1);fclose(f2);
}
3.从上一题中建立的名为“f1.txt”的磁盘文件中读取120个字符,并显示在屏幕上。
#include“stdio.h”
voidmain()
{FILE*fp;
inti;
if((fp=fopen(“f1.txt”,”r”))==NULL)
{printf(“Cannotopenfile!
\n”);exit(0);}
for(i=1;i<=120;i++)putchar(fgetc(fp));
fclose(fp);
}
4.编一个程序,将磁盘中当前目录下名为“cow1.txt”的文本文件复制在同—目录下,
文件名改为“ccw2.txt”。
#include“stdio.h”
voidmain()
{FILE*f1,*f2;
if((f1=fopen(“ccw1.txt”,”r”))==NULL)
{printf(“Cannotopenfile!
\n”);exit(0);}
if((f2=fopen(“ccw2.txt”,”w”))==NULL)
{printf(“Cannotopenfile!
\n”);exit
(1);}
while(!
feof(f1))fputc(fgetc(f1),f2);
}
5.把文本文件d1.dat复制到文本文件d2.dat中,要求仅复制d1.dat中的英文字符。
#include
voidmain()
{FILE*fpd1,*fpd2;charch;
if((fpd1=fopen("d1.dat","r"))==NULL)
{printf(“Cannotopenfile!
\n”);exit(0);}
if((fpd2=fopen("d2.dat","w");
{printf(“Cannotopenfile!
\n”);exit
(1);}
while(fscanf(fpd1,"%c",&ch)!
=EOF)
if(ch>='A'&&ch<='Z'||ch>='a'&&ch<='z')fprintf(fpd2,"%c",ch);
fclose(fpd1);fclose(fpd2);
}
6.编程,把文本文件d1.dat复制到d2.dat(其中空格字符不复制)。
#include
voidmain()
{FILE*fpd1,*fpd2;charch;
if((fpd1=fopen("d1.dat","r"))==NULL)
{printf(“Cannotopenfile!
\n”);exit(0);}
if((fpd2=fopen("d2.dat","w"))==NULL)
{printf(“Cannotopenfile!
\n”);exit
(1);}
while(fscanf(fpd1,"%c",&ch)!
=EOF)
if(ch!
='')fprintf(fpd2,"%c",ch);
fclose(fpd1);fclose(fpd2);
}
7.编程,把文本文件d1.dat复制到d2.dat(其中大写英文字母要转换为小写字母)。
#include
voidmain()
{FILE*fpd1,*fpd2;charch;
if((fpd1=fopen("d1.dat","r"))==NULL)
{printf(“Cannotopenfile!
\n”);exit(0);}
if((fpd2=fopen("d2.dat","w")==NULL)
{printf(“Cannotopenfile!
\n”);exit
(1);}
while(fscanf(fpd1,"%c",&ch)!
=EOF){
if(ch>='A'&&ch<='Z')ch=ch+32;
fprintf(fpd2,"%c",ch);
}
fclose(fpd1);fclose(fpd2);
}
8.把文本文件d1.dat复制到文本文件d2.dat中,要求仅复制d1.dat中除英文字符和
数字以外的其它内容。
#include
voidmain()
{FILE*fpd1,*fpd2;charch;
if((fpd1=fopen("d1.dat","r"))==NULL)
{printf(“Cannotopenfile!
\n”);exit(0);}
if((fpd2=fopen("d2.dat","w"))==NULL)
{printf(“Cannotopenfile!
\n”);exit
(1);}
while(fscanf(fpd1,"%c",&ch)!
=EOF)
if(!
(ch>='A'&&ch<='Z'||ch>='a'&&c