6-1 写文章(*) (10分)

6-1 写文章(*) (10分)

请编写函数,从键盘输入一篇文章,将其写入到文件中。
函数原型

void WriteArticle(FILE *f);

说明:参数 f 为文件指针。函数从键盘输入一篇文章(由若干行文字组成,以 Ctrl+Z 结束),将其写入 f 所指示的文件中。
裁判程序

#include 
#include 

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 WriteArticle(FILE *f)
{
	char ch;
	ch=getchar();		//输入到程序
	while(ch!=EOF)
	{
		fputc(ch,f);	//储存到文件
		ch=getchar();
	}
}

你可能感兴趣的:(6-1 写文章(*) (10分))