一、填空题
1.设一文件指针为fp,则关闭该文件的语句是fclose(fp) 。
2.在C语言中,文件的数据存放有两种形式,一种是二进制形式,另一种是
ASCII形式。
二、二、选择题
1.要打开A盘上user子目录下名为abc.txt的文本文件进行读、写操作,下
面符合此要求的函数调用是( B )
A) fopen("A:\user\abc.txt","r") B) fopen("A:\\user\\abc.txt","r+")
C) fopen("A:\user\abc.txt","rb") C) fopen("A:\\user\\abc.txt","w")
二、填程序:
1.下面程序用变量count统计文件中字符的个数。
请在空白处填上适当内容。
#include <stdio.h>
main( )
{ FILE *fp;
long count=0;
if((fp=fopen(“letter.dat”, “r”))==NULL)
{ printf(“Can’t open file!\n”);
exit(0);
}
while(!feof(fp))
{ fgetc(fp);
count++ ;
}
printf(“count=%ld\n”,count);
fclose(fp) ;
}
2.下面程序由终端键盘输入字符,存放到文件中,用!结束输入。
请在空白处填上适当内
容。
#include <stdio.h>
main( )
{ FILE *fp;
char ch, fname[10];
printf(“Input name of file\n”);
gets(fname);
if((fp=fopen(fname,”w”))==NULL)
{ printf(“Cannot open\n”); exit(0);}
printf(“Enter data:\n”);
while( ( ch=getchar() ) !=’!’) fputc(ch, fp );
fclose(fp);
}。