当前位置:网站首页>6-2 writing articles (*)

6-2 writing articles (*)

2022-06-11 17:37:00 Tomatoes_ Menon

6-2 Write an article (*)

fraction  10

Full screen browsing topics

Switch layout

author   Li Xiang

Company   Hubei Institute of Economics

Please write a function , Enter an article from the keyboard , Write it to a file .

The function prototype

void WriteArticle(FILE *f);

explain : Parameters f For file pointer . Function to enter an article from the keyboard ( Composed of several lines of text , With Ctrl+Z end ), Write it to f In the indicated file .

judging procedures

#include <stdio.h>
#include <stdlib.h>

void WriteArticle(FILE *f);

int main()
{
    FILE *f;
    f = fopen("Article.txt", "w");
    if (!f)
    {
        puts(" The file could not be opened !");
        exit(1);
    }

    WriteArticle(f);

    if (fclose(f))
    {
        puts(" File cannot be closed !");
        exit(1);
    }
    puts(" File saved successfully !");
    return 0;
}

sample input

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.

Tips : Press enter after the last line is entered , Press at the beginning of the next line Ctrl + Z ( The screen shows ^Z), Then press the Enter key to end the input .

sample output

 File saved successfully !

open “Article.txt” file , View file contents .

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 data;
    data=getchar();
    while(data!=EOF){
        fputc(data,f);
        data=getchar();
    }
}

 

原网站

版权声明
本文为[Tomatoes_ Menon]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206111717106220.html