从键盘输入一些字符,把它们保存在磁盘文件file1.dat中,同时显示在屏幕上,直到用户输入一个“#”为止。

解题思路:用getchar()从键盘输入字符,用fputc()将字符保存到文档中,用putchar()显示在屏幕上。

程序运行结果示例:
Input your strings and press #
疏影横斜水清浅,暗香浮动月黄昏。#
疏影横斜水清浅,暗香浮动月黄昏。

输入提示:"Input your strings and press #\n"

#include <stdio.h>

int main()
{
    char c;
    FILE *fp;
    printf("Input your strings and press #\n");
     
    fp = fopen("file1.dat", "w+");
    while ((c=getchar())!='#') {
        fputc(c, fp);
    }
    fclose(fp);

    fp = fopen("file1.dat","r");
    while(!feof(fp)) {
        c = fgetc(fp);
          putchar(c);
    }
    fclose(fp);
    
    return 0;
}