1-1
文件指针指向文件缓冲区中文件数据的存取位置。
(1分)
F
作者
张泳
单位
浙大城市学院
1-1
答案正确
(1 分)
1-2
文件的读函数是从输入文件中读取信息,并存放在内存中。
(1分)
T
作者
王从银
单位
吉首大学
1-2
答案正确
(1 分)
1-3
从文件的逻辑结构上看,c语言把文件看作数据流,并将数据按顺序以一维方式组织存储。
(1分)
T
作者
张泳
单位
浙大城市学院
1-3
答案正确
(1 分)
1-4
对于缓冲文件系统,在进行文件操作时,系统自动为每一个文件分配一块文件内存缓冲区(内存单元)。
(1分)
T
作者
张泳
单位
浙大城市学院
1-4
答案正确
(1 分)
1-5
随机操作只适用于二进制文件。
(1分)
F
作者
王从银
单位
吉首大学
1-5
答案正确
(1 分)
1-6
文件指针和位置指针都是随着文件的读写操作在不断改变。
(1分)
F
作者
王从银
单位
吉首大学
1-6
答案正确
(1 分)
1-7
随机操作只适用于文本文件。
(1分)
F
作者
王从银
单位
吉首大学
1-7
答案正确
(1 分)
1-8
c语言源程序是文本文件,目标文件和可执行文件是二进制文件。
(1分)
T
作者
张泳
单位
浙大城市学院
1-8
答案正确
(1 分)
1-9
以“a”方式打开一个文件时,文件指针指向文件首。
(1分)
F
作者
王从银
单位
吉首大学
1-9
答案正确
(1 分)
4-1
C语言中,在成功打开一个文件后,可以使用
文件指针
来获取文件缓冲区的FILE结构信息。
作者
张泳
单位
浙大城市学院
4-1
答案正确
(1 分)
4-2
根据数据存储的编码形式,C语言中处理的数据文件通常为
文本
文件和
二进制
文件两种。
作者
张泳
单位
浙大城市学院
4-2
答案正确
(2 分)
4-3
判断文件指针是否已经到了文件尾部的函数是
feof
。
作者
张泳
单位
浙大城市学院
4-3
答案正确
(1 分)
4-4
fgets(s,n,fp);语句用来从fp所指示的文本文件中读取字符串s,该语句最多读取
n-1
个字符。
作者
张泳
单位
浙大城市学院
4-4
答案正确
(1 分)
6-2 写文章(*) (10分)
请编写函数,从键盘输入一篇文章,将其写入到文件中。
函数原型
void WriteArticle(FILE *f);
说明:参数 f 为文件指针。函数从键盘输入一篇文章(由若干行文字组成,以 Ctrl+Z 结束),将其写入 f 所指示的文件中。
裁判程序
#include <stdio.h>
#include <stdlib.h>
void WriteArticle(FILE *f);
int main()
{
FILE *f;
f = fopen("Article.txt", "w");
if (!f)
{
puts("文件无法打开!");
exit(1);
}
WriteArticle(f);
if (fclose(f))
{
puts("文件无法关闭!");
exit(1);
}
puts("文件保存成功!");
return 0;
}
/* 你提交的代码将被嵌在这里 */
输入样例
Poor Old Gentleman
An old gentleman was walking slowly along a street one day when he saw a little
boy who was trying to reach a doorbell which was too high for him. He was a
kind-hearted old man, so he stopped and said, "I will ring the bell for you,"
and then he pulled the bell so hard that it could be heard all over the house.
The little boy looked up at him and said, "Now we will run away. Come on." And
before the old gentleman knew what was happening, the naughty boy had run round
the corner of the street, leaving the man to explain to the angry owner of the
house why he had rung the bell.
提示:最后一行输入完以后打回车键,在下一行的开头按下 Ctrl + Z (屏幕显示为 ^Z),然后再打回车键结束输入。
输出样例
文件保存成功!
打开“Article.txt”文件,查看文件内容。
Article.txt
Poor Old Gentleman
An old gentleman was walking slowly along a street one day when he saw a little
boy who was trying to reach a doorbell which was too high for him. He was a
kind-hearted old man, so he stopped and said, "I will ring the bell for you,"
and then he pulled the bell so hard that it could be heard all over the house.
The little boy looked up at him and said, "Now we will run away. Come on." And
before the old gentleman knew what was happening, the naughty boy had run round
the corner of the street, leaving the man to explain to the angry owner of the
house why he had rung the bell.
void WriteStory(FILE *f)
{char c;
while ((c=fgetc (stdin)) != EOF)
{
fputc(c,f);
}
}
6-3 读文章(*) (10分)
请编写函数,从文件中读出文章,将其输出到屏幕上。
函数原型
void ReadArticle(FILE *f);
说明:参数 f 为文件指针。函数读出 f 所指示文件中的文章,将其输出到屏幕上。
裁判程序
#include <stdio.h>
#include <stdlib.h>
void ReadArticle(FILE *f);
int main()
{
FILE *f;
f = fopen("Article.txt", "r");
if (!f)
{
puts("文件无法打开!");
exit(1);
}
ReadArticle(f);
if (fclose(f))
{
puts("文件无法关闭!");
exit(1);
}
return 0;
}
/* 你提交的代码将被嵌在这里 */
打开 Windows 的记事本软件,复制下面的文字内容,保存文件并命名为“Article.txt”。
Article.txt
A Cure for a Headache
One day a man went into a chemist's shop and said, "Have you anything to cure a
headache?"
The chemist took a bottle from a shelf, held it under the gentleman's nose and
took out the cork. The smell was so strong that tears came into the man's eyes
and ran down his cheeks.
"What did you do that for?" he said angrily, as soon as he could get back his
breath.
"But that medicine has cured your headache, hasn't it?" said the chemist.
"You fool," said the man, "It's my wife that has the headache, not me!"
输入样例
(无)
输出样例
A Cure for a Headache
One day a man went into a chemist's shop and said, "Have you anything to cure a
headache?"
The chemist took a bottle from a shelf, held it under the gentleman's nose and
took out the cork. The smell was so strong that tears came into the man's eyes
and ran down his cheeks.
"What did you do that for?" he said angrily, as soon as he could get back his
breath.
"But that medicine has cured your headache, hasn't it?" said the chemist.
"You fool," said the man, "It's my wife that has the headache, not me!"
void ReadStory(FILE *f)
{
char ch;
ch=fgetc(f);
while(ch!=EOF)
{
putchar(ch);
ch=fgetc(f);
}
//remove("Story.txt");
}
6-4 写字母(*) (10分)
请编写函数,将大写字母写入文件中。
函数原型
void WriteLetter(FILE f, int n);6-4 写字母() (10分)
请编写函数,将大写字母写入文件中。
函数原型
void WriteLetter(FILE *f, int n);
说明:参数 f 为文件指针,n 为字母数目(1 ≤ n ≤ 26)。函数将前 n 个大写英文字母写入 f 所指示的文件中。
裁判程序
#include <stdio.h>
#include <stdlib.h>
void WriteLetter(FILE *f, int n);
int main()
{
FILE *f;
int n;
f = fopen("Letter.txt", "w");
if (!f)
{
puts("文件无法打开!");
exit(1);
}
scanf("%d", &n);
WriteLetter(f, n);
if (fclose(f))
{
puts("文件无法关闭!");
exit(1);
}
puts("文件保存成功!");
return 0;
}
/* 你提交的代码将被嵌在这里 */
样例输入
3
输出样例
文件保存成功!
打开“Letter.txt”文件,查看文件内容:
Letter.txt
ABC
说明:参数 f 为文件指针,n 为字母数目(1 ≤ n ≤ 26)。函数将前 n 个大写英文字母写入 f 所指示的文件中。
裁判程序
#include <stdio.h>
#include <stdlib.h>
void WriteLetter(FILE *f, int n);
int main()
{
FILE *f;
int n;
f = fopen("Letter.txt", "w");
if (!f)
{
puts("文件无法打开!");
exit(1);
}
scanf("%d", &n);
WriteLetter(f, n);
if (fclose(f))
{
puts("文件无法关闭!");
exit(1);
}
puts("文件保存成功!");
return 0;
}
/* 你提交的代码将被嵌在这里 */
样例输入
3
输出样例
文件保存成功!
打开“Letter.txt”文件,查看文件内容:
Letter.txt
ABC
void WriteLetter(FILE *f, int n)
{
int i;
char a,ch='A';
for(i=0;i<n;i++)
{ch='A'+i;
fprintf(f,"%c",ch);}
fclose(f);
}