C语言从键盘输入字符串存储文本#作为结束符

代码:

#include <stdio.h>
#include <string.h>

#define N 1024


int main(int argc, char* argv[])
{
    FILE* fp;
    fp = fopen("file.txt", "w");
    if(!fp){
        printf("file open failed.");
        return 0;
    }

    char c;
    printf("please input a string end with #:\n");
    c = getchar();
    while(c != '#'){
        fputc(c, fp);
        c = getchar();
    }

    fclose(fp);

    return 0;
}

 

posted @ 2021-07-17 14:46  朱小勇  阅读(434)  评论(0编辑  收藏  举报