一、概念题
1. fopen函数的返回值是返回指向该流的文件指针。
2. 文件打开方式为"r+",文件打开后,文件读写位置在头。
3. 文件打开方式为"a",文件打开后,文件读写位置在尾。
4. 表达式“fgetc(fpn)”的值为带回所读的字符或EOF 。
5. 表达式“fgets(a, 10, fpn)”的值为a地址或NULL 。
6. 函数fscanf的返回值为输入项个数或EOF 。
7. 函数fread的返回值为所读入数据的个数或0 。
8. 表达式“fscanf(fpn, "%f", &x)”的值为-1时,函数feof()的值为 1 。
二、判断题
1. 若文件型指针fp已指向某文件的末尾,则函数feof(fp)的返回值是0(F )。
2. 不能用“r”方式打开一个并不存在的文件(T )。
3. FILE *fp;的功能是,将fp定义为文件型指针(T )。
4. “文件”是指存储在外部介质上数据的集合(T)。
5. “文件”根据数据的组织形式可以分为ASCII文件和十进制文件(F )。
6. 用“r”方式打开的文件只能用于向计算机输入数据(F)。
7. 用“wb”方式打开的文件为输出打开一个ASCII文件(F )。
8. 文件结束标志EOF(-1)表示已经遇到文件结束符(T)。
9. fseek函数用于文件读写时的文件指针定位(F)。
三、单选题
1. 以下叙述中不正确的是(D )。
A. C语言中的文本文件以ASCⅡ码形式存储数据
B. C语言中对二进制文件的访问速度比文本文件快
C. C语言中,随机读写方式不适用于文本文件
D. C语言中,顺序读写方式不适用于二进制文件
2. 若用fopen()函数打开一个已经存在的文本文件,保留该文件原有数据且可以读也可以写,则文件的打开模式为( C )。
A.“ab+” B.“w+” C.“a+” D.“a”
3. 若想对文本文件只进行读操作,打开此文件的方式为(A )。
A."r" B."W" C."a" D."r+"
4. 用(A )函数打开文件,操作完毕后用(C )函数关闭它。
A.fopen
B.open
C.fclose
D.close
5. 如果要打开C盘file文件夹下的abc.dat文件,fopen函数中第一个参数应为( D )。
A. c:file\abc.dat
B. c:\file\abc.dat
C. "c:\file\abc.dat"
D. "c:\\file\\abc.dat"
6. 以“只读”方式打开文本文件c:\xy.txt,下列语句中哪一个是正确的(B )。
A. fp=fopen("c:\\xy.txt","a");
B. fp=fopen("c:\\xy.txt","r");
C. fp=fopen("c:\\xy.txt","wb");
D. fp=fopen("c:\xy.txt","r");
7. fseek函数可以实现的操作是(A)。
A. 改变文件的位置指针的当前位置
B. 文件的顺序读写
C. 文件的随机读写
D. 以上都不对
8. 检测fp文件流的文件位置指针在文件头的条件是(B )。
A. fp=0
B. ftell(fp)=0
C. fseek(fp,0,SEEK_SET)
D. feof(fp)
9. 以下程序企图把从终端输入的字符输出到名为abc.txt的文件中,直到从终端读入字符#号时结束输入和输出操作,但程序有错。
#include “stdio.h”
main()
{ FILE *fout; char ch;
fout=fopen('abc.txt','w');
ch=fgetc(stdin);
while(ch!='#')
{ fputc(ch,fout);
ch=fgetc(stdin);
}
fclose(fout);
}
出错的原因是A
A. 函数fopen调用形式错误
B. 输入文件没有关闭
C. 函数fgetc调用形式错误
D. 文件指针stdin没有定义
四、程序填空题
1. 以下程序将一个磁盘文件中的信息复制到另一个磁盘文件中。
#include "stdio.h"
main( )
{ FILE *in, *out;
char ch;
char infile[10], outfile[10];
printf("Enter the infile name\n");
scanf("%s", 【1】 infile );
printf("Enter the outfile name\n");
scanf("%s", outfile);
if (( 【2】in=fopen(“””) )==NULL) {
printf("can not open infile %s\n", infile);
exit(0); }
if ((out = fopen(outfile, " 【3】"))==NULL){
printf("can not open outfile %s\n", outfile);
exit(0); }
while( 【4】)
fputc( 【5】, out);
fclose(in);
fclose(out);
}
2. 当前目录下存放着文本文件from.txt, 将其中除数字以外的内容显示在屏幕上。
#include < stdi0.h >
#include < stdlib.h >
void main ()
{ FILE *fr;int ch;
if( 【1】){
printf("Can not open file-- form.txt.\n");
exit (0);
}
while (!feof(fr)){
【2】 ;
if (【3】)
putchar(ch);
}
fclose(fr);
}
3. 从键盘输入一批以-1结束的整数,将其中的奇数写入当前目录下的文本文件res.txt。
#include
#include
void main()
{ int x;
【1】;
if((fp=fopen("res.txt","w"))==NULL) {
printf("Can not open file! \n ");
exit(0);
}
scanf("%d", &x);
while( 【2】){
if(x%2!=0) 【3】;
【4】;
}
fclose(fp);
}
4. 本文件a.dat、b.dat中每行存放一个数且均按从小到大存放。下列程序将这两个文件中的数据合并到c.dat,文件c.dat中的数据也要从小到大存放。请填空,将程序补充完整、正确(若文件a.dat数据为1、6、9、18、27、35,文件b.dat数据为
10、23、25、39、61,则文件c.dat中数据应为
1、6、9、10、18、23、25、27、35、39、61)。
# include
# include
void main()
{ FILE *f1, *f2, *f3; int x, y;
if((f1=fopen("a.dat", "r"))==NULL)
{ printf("Can not open a.dat!\n"); exit(0); }
if((f2=fopen("b.dat", "r"))==NULL)
{ printf("Can not open b.dat!\n"); exit(0); }
if( 【1】)==NULL}
{ printf("Can not open c.dat!\n"); exit(0); }
fscanf(f1, "%d", &x); 【2】;
while(!feof(f1)&&!feof(f2))
if( 【3】)
{ fprintf(f3, "%d\n", x); fscanf(f1, "%d", &x); }
else { fprintf(f3, "%d\n", y); fscanf(f2, "%d", &y); }
if(feof(f1))
{ 【4】fprintf(f3,”%d”,y);
while(!feof(f2))
{ fscanf(f2, "%d", &y); 【5】; }
}
else { fprintf(f3, 【6】);
while(!feof(f1))
{ 【7】; fprintf(f3, "%d\n", x); }
}
fclose(f1); fclose(f2); fclose(f3);
}
五、程序阅读题
1. 已有文本文件test.txt,其中的内容为:Hello,everyone!。以下程序中,文件test.txt已正确为"读"而打开,由文件指针fr指向该文件,则程序的输出结果是:
#include "stdio.h"
main()
{ FILE *fr; char str[40];
……
fgets(str,5,fr);
printf("%s\n",str);
fclose(fr);
}
hell
2. 假设读写文件的操作能正常完成,则程序的输出结果是:
#include
#include
void main ()
{ char ch,*s=”ACEDB”;
int x;
FILE *in;
if ((in=fopen(“file.txt”,”w”))!=NULL)
while(*s!=’\0’)
fputc(*s++,in);
fclose(in);
if ((in=fopen(“file.txt”,”r”))!=NULL)
while ((ch=fgetc(in))!=EOF){
switch(ch) {
case ’A’:x=95; break;
case ’B’:x=85; break;
case ’C’:x=75; break;
case ’D’:x=60; break;
default:x=0;break;
}
print(“%d#”,x);
}
fclose(in);
}
3. 以下程序完成的功能是:
#include "stdio.h"
main()
{
FILE *fp;
char ch;
char filename[10];
printf("Input filename\n");
scanf("%s\n", filename);
if ((fp = fopen(filename, "w"))==NULL) {
printf("can not open file %s\n", filename);
exit(0); }
ch = getchar();
while(ch != '#') {
fputc(ch,fp);
putchar(ch);
ch = getchar();
}
fclose(fp);
}
4. 以下程序完成的功能是:
#include "stdio.h"
main()
{ FILE *fp;
char ch;
fp = fopen("c:\\TC\\FILE\\test.txt", "r");
if (fp == NULL) {
printf("can not open test.txt \n");
exit(0); }
ch = fgetc(fp);
while(ch != EOF)
{ putchar(ch);
ch = fgetc(fp); }
fclose(fp);
}
5. 以下程序完成的功能是:
#include "stdio.h"
main()
{
FILE *in, *out;
char ch;
char infile[10], outfile[10];
printf("Enter the infile name\n");
scanf("%s", infile);
printf("Enter the outfile name\n");
scanf("%s", outfile);
if ((in = fopen(infile, "r"))==NULL)
{ printf("can not open infile %s\n", infile);
exit(0); }
if ((out = fopen(outfile, "w"))==NULL)
{ printf("can not open outfile %s\n", outfile);
exit(0); }
while(!feof(in))
fputc(fgetc(in), out);
fclose(in);
fclose(out);
}
6. 以下程序执行后输出结果是:。
#include
main( )
{ FILE *fp; int i,k=0,n=0;
fp=fopen("d1.dat","w");
for(i=1;i<4;i++) fprintf(fp,"%d",i);
fclose(fp);
fp=fopen("d1.dat","r");
fscanf(fp,"%d%d",&k,&n); printf("%d %d\n",k,n);
fclose(fp);
}
7. 以下程序执行后输出结果是:。
#include
main( )
{ FILE *fp;
int i,a[4]={1,2,3,4},b;
fp=fopen("data.dat","wb");
for(i=0;i<4;i++) fwrite(&a[i],sizeof(int),1,fp);
fclose(fp);
fp=fopen("data.dat","rb");
fseek(fp,-2L*sizeof(int).SEEK_END);/*使位置指针从文件尾向前移2*sizeof(int)字节*/ fread(&b,sizeof(int),1,fp);/*从文件中读取sizeof(int)字节的数据到变量b中*/
fclose(fp);
printf("%d\n",b);
}
3
8. 设head是node类型的全程量,以head为头指针的链表各节点的值如下图所示。head→ 3 ()→4()→5()→6()→7 NULL ,调用fun(head)返回值是:#include
#define LEN sizeof(struct node)
struct node{
int nun;
struct node *next;
};
int fun(struct node*h)
{int k=0;
struct node*p=h;
while(p!=NULL){
if(p->next!=NULL)k+=p->num;
p=p->next;
}
return k;
}
void main()
{ struct node*head ,*p1,*p2;
int i;
head=(struct node *) malloc(LEN);p1=head;
for(i=3;i<=7;i++)
{p2=(struct node*)malloc(LEN);p1->next=p2;
p2->num=i;p2->next=NULL;
p1=p2;printf(“%d\n”,fun(head->next));
}}
18
9. 以下程序执行后输出结果是:。
# include
void main()
{ struct info {int data; struct info *pn;};
info base, p;
base=NULL;
for(int i=0; i<10; i++)
{ p=(struct info*)malloc(sizeof(struct info)); p->data=i+1;
p->pn=base; base=p;
}
p=base;
while(p!=NULL)
{ printf("%2d", p->data); p=p->pn;
}
printf("\n");
}
1 2 3 4 5 6 7 8 9 10
六、程序设计题
1. 从C盘根目录下名为“xhar
2.txt“的文本文件中读取前10个字符,依次显示在屏幕上。如果文本文件中不足10个字符,则有多少个字符读取并显示多少个字符。
提示:参考程序段:
for(i=0;i<10;i++)
{ if(feof(fp)) break;
c=fgetc(fp);
putchar();
}
2. 从键盘上读取3个字符串,依次写入D盘根目录下名为“string1.txt”的文本文件。
提示:参考程序段:
for(i=0;i<3;i++)
{ gets(s1[i]);
fputs(s1[i],fp); }
3. 有两个磁盘文件a.dat和b.dat,要求产生一个新的文件c.dat,将b.dat中的数据追加到
a.dat后面,并存入到c.dat中。
提示:设两个磁盘文件为文本文件。以读的方式打开a.dat和b.dat,以写的方式创建
c.dat。先将b.dat的数据复制到c.dat,再将a.bat的数据复制到c.dat。
4. 从键盘输入一些字符,逐个把它们存入磁盘文件test.txt中去,直到输入一个#为止。
提示:利用fputc函数将字符写入到磁盘文件中。
5. 编程统计C盘Mydir文件夹中文本文件data.txt中字符'$'出现的次数。并将统计结果写入到文本文件C:\Mydir\res.txt中。
6. 读入一个文件,输出其中最长的一行的行号和内容。
提示:以硬回车键'\n'作为行的结束标志。
7. 编写程序将全班同学的姓名、地址和电话号码写到一个文件class.dat中。
提示:学生的信息可以存放到结构体数组,以fwrite函数写数据到文件中。
8. 将6题产生的class.dat文件中的数据按姓名从低到高排列输出到显示器上,并把排了序的数据重新写入到文件class1.dat中。
提示:以fread函数从文件中读入学生的信息,并存放到结构体数组,在数组中进行排序。排序完毕再写入文件中。
9. 利用7题产生的class1.dat文件,编程实现从中直接读取第三个同学的数据。
10. 在7题产生的class1.dat文件中插入一个新生的数据,要求插入后的文件数据仍然按姓名顺序排列。
提示:先将数据读入到数组中,插入新的数据后,再写入到文件中。