6-3 写文章(*)

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

函数原型

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 WriteArticle(FILE *f)
{
    char ch;
    ch=getchar();
    while(ch!=EOF)
    {
        fputc(ch,f);
        ch=getchar();
    }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值