C程序将字母附加到文件末尾

时间:2015-10-12 17:22:06

标签: c file file-io while-loop append

打开现有文本文件并附加新信件 只要用户想要添加新文件,就可以访问此文件 写信给文件。

使用while循环 使用fopen(“filename”,“a”);

我不知道从哪里开始。我理解如何将单个字母,单词或短语附加到文件中,但我不知道在其上进行其他操作。非常感谢任何帮助。

好的,所以我想分别编写while循环部分,然后让它打印输出,看看我是否在正确的轨道上......

这是我到目前为止所拥有的......

我还需要帮助的是如何结束while循环(通过使用特定字符)

如何将其整合到文件格式中,并将其打印到文件中。



#include <stdio.h>

int main()
{

    int true;
    char mychar;
    char NUL;
    NUL =000;


    while(true)
    {
        printf("\nEnter a letter to append to file A.txt: ");
        scanf(" %c", &mychar);
        if(mychar == (char)000)
            break;
    }

    return 0;
}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

使用while循环来实现此目的:

#include <stdio.h>

int main(void)
{
    char c;

    FILE *file;

    file = fopen("file.txt","a");

    //you can use EOF instead if '\n'
    while( (c = getc(stdin)) != '\n' )
    {
        putc(c,file);
    }
    fclose(file);
    return 0;
}