[VIP专享]第十章 文件
- 格式:pdf
- 大小:122.51 KB
- 文档页数:10
一、概念题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.fopenB.openC.fcloseD.close5. 如果要打开C盘file文件夹下的abc.dat文件,fopen函数中第一个参数应为( D )。
A. c:file\abc.datB. c:\file\abc.datC. "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=0B. ftell(fp)=0C. 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);}出错的原因是AA. 函数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 <stdio.h>#include <stdlib.h>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 <stdio.h># include <stdlib.h>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);}hell2. 假设读写文件的操作能正常完成,则程序的输出结果是:#include <stdio.h>#include <stdlib.h>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. 以下程序执行后输出结果是:。