C写完后附加到文件

时间:2014-11-25 02:45:55

标签: c file-io

我正在测试用于操作文件的基本功能。

我尝试先打开/关闭文件来创建它,然后再次打开/关闭它以附加到它。最后,我打印出文件中的内容。

我的代码目前如下所示:

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

int main()
{
  FILE * file;
  char mark;

  /* WRITING: */
  file= fopen("goodbye.c","w");
  if(!file)
    { printf("Couldn't open file.\n");
      exit(EXIT_FAILURE); }
  printf("Enter data to write to .c file:");
  while((mark= getchar())!=EOF)
    {
      putc(mark,file);
    }
  fclose(file);

  /* APPENDING: */
  file= fopen("goodbye.c","a");
  if(!file)
    { printf("Couldn't open file.\n");
      exit(EXIT_FAILURE); }
  char add;
  scanf("%c",add);
  putc(add,file);
  fclose(file);

  /* READING: */
  file= fopen("goodbye.c","r");
  if(!file)
    { printf("Couldn't open file.\n");
      exit(EXIT_FAILURE); }
  while((mark= getc(file))!= EOF)
    {
      printf("%c",mark);
    }
  fclose(file);
}

有了这个,我无法附加到该文件。当使用getchar()时,我在第一次写完后输入ctrl + d。在此之后,它继续打印我刚写的内容,而不是给我机会追加到文件。 ctrl + d会以某种方式中断scanf吗? 以及如何获得我正在寻找的结果?

1 个答案:

答案 0 :(得分:3)

您的代码只允许您在文件中附加单个字符,这有点吝啬。如果文本文件的最后一行没有以换行符结尾,它也可能(至少在理论上)导致某些系统出现问题,如果你添加了换行符以外的其他内容,它就不会出现问题。也许你需要一个循环来读取多个字符?

此外,由于您不会在EOF之前停止初始输入,因此您需要清除错误&#39;在stdinclearerr(stdin)允许进一步输入。这适用于Mac OS X 10.10.1 Yosemite;它应该在其他Unix系统上运行相同。我无法自信地回答基于Windows的代码,除非它使用像Cygwin这样的东西来模拟Unix,但我希望它也能以相同的方式工作,即使使用MSVC也是如此。

顺便说一下,我的编译器抱怨&scanf()的调用中丢失了char add; scanf("%c",add);

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

int main(void)
{
    FILE *file;
    char mark;

    /* WRITING: */
    file = fopen("goodbye.c", "w");
    if (!file)
    {
        printf("Couldn't open file.\n");
        exit(EXIT_FAILURE);
    }
    printf("Enter data to write to .c file:");
    while ((mark = getchar()) != EOF)
    {
        putc(mark, file);
    }
    fclose(file);
    printf("EOF 1\n");

    /* APPENDING: */
    file = fopen("goodbye.c", "a");
    if (!file)
    {
        printf("Couldn't open file.\n");
        exit(EXIT_FAILURE);
    }
    clearerr(stdin);
    char add;
    while (scanf("%c", &add) == 1)
        putc(add, file);
    fclose(file);
    printf("EOF 2\n");

    /* READING: */
    file = fopen("goodbye.c", "r");
    if (!file)
    {
        printf("Couldn't open file.\n");
        exit(EXIT_FAILURE);
    }
    while ((mark = getc(file)) != EOF)
    {
        printf("%c", mark);
    }
    fclose(file);
    return 0;
}

如果您的编译器没有抱怨,请调高警告级别或获得更好的编译器。

此代码可以正常运行:

scanf()

唯一的实质性更改是在getchar()周围添加循环 - 但坦率地说,再次使用scanf()会更好,就像在第一个输入循环中一样 - 将调用修复为printf() ,添加在检测到EOF时报告的两个clearerr(stdin);语句,并包括clearerr(stdin)以允许输入继续。

示例输出

没有Enter data to write to .c file:Happiness is a bug-free program. Happiness is seldom attained. EOF 1 EOF 2 Happiness is a bug-free program. Happiness is seldom attained. 的代码:

clearerr(stdin)

代码Enter data to write to .c file:Happiness is a bug-free program. Happiness is seldom attained. EOF 1 But it helps when you add the clearerr(stdin) to this one. EOF 2 Happiness is a bug-free program. Happiness is seldom attained. But it helps when you add the clearerr(stdin) to this one.

{{1}}