C语言-文件中追加字符(C primer plus 13章)

C语言-文件中追加字符(C primer plus 13章),第1张

C语言-文件追加字符(C primer plus 13章)

Hello, 大家好,我是爱吃香蕉的猴子,记录一下addaword.c程序


#include 
#include 
#include 
#define MAX 40

int main(void)
{
    FILE *fp;
    char words[MAX];

    if((fp = fopen("words", "a+")) == NULL)
    {
        fprintf(stdout, "Can't open words file.n");
        exit(1);
    }
    puts("Enter words to add to the file: press the Enter press at the begining of a line to terminate.");

    while((fscanf(stdin, "%40s", words) == 1) && (words[0] != '#'))
        fprintf(fp, "%sn", words);

    puts("File contents:");
    rewind(fp);//返回到文件开始处读取
    while(fscanf(fp, "%s", words) == 1)
        puts(words);

    if(fclose(fp) != 0)
        fprintf(stdout, "Error closing file.n");

    return 0;
}

                                 Code的搬运工V1.0

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/4950132.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-13
下一篇 2022-11-13

发表评论

登录后才能评论

评论列表(0条)

保存