Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C在写入后追加到文件_C_File Io - Fatal编程技术网

C在写入后追加到文件

C在写入后追加到文件,c,file-io,C,File Io,我正在测试操作文件的基本功能 我尝试首先打开/关闭一个文件来创建它,然后再次打开/关闭它以附加到它。最后,我打印出文件中的内容 我的代码当前如下所示: #include <stdio.h> #include <stdlib.h> int main() { FILE * file; char mark; /* WRITING: */ file= fopen("goodbye.c","w"); if(!file) { printf("Could

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

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

我的代码当前如下所示:

#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);
}
#包括
#包括
int main()
{
文件*文件;
字符标记;
/*写作:*/
file=fopen(“再见,c”,“w”);
如果(!文件)
{printf(“无法打开文件。\n”);
退出(退出失败);}
printf(“输入要写入.c文件的数据:”);
而((mark=getchar())!=EOF)
{
putc(标记、文件);
}
fclose(文件);
/*附加:*/
file=fopen(“再见,c”,“a”);
如果(!文件)
{printf(“无法打开文件。\n”);
退出(退出失败);}
添加字符;
scanf(“%c”,添加);
putc(添加,文件);
fclose(文件);
/*阅读:*/
file=fopen(“再见,c”,“r”);
如果(!文件)
{printf(“无法打开文件。\n”);
退出(退出失败);}
而((mark=getc(file))!=EOF)
{
printf(“%c”,标记);
}
fclose(文件);
}
有了这个,我无法附加到文件中。在使用getchar()时,我首先在完成编写后键入ctrl+d。在这之后,它继续打印出我刚刚写的内容,而不是给我附加到文件的机会。ctrl+d是否以某种方式中断了scanf?
如何得到我想要的结果?

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

此外,由于在EOF之前不会停止初始输入,因此需要使用
clearr(stdin)
清除
stdin上的“error”(错误),以便进行进一步的输入。这在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;
}
使用clearerr(标准输入法)编码

输入要写入.c文件的数据:Happiness是一个没有bug的程序。
幸福难得。
EOF 1
但是,当您将clearerr(stdin)添加到此文件中时,它会有所帮助。
EOF 2
幸福是一个没有bug的程序。
幸福难得。
但是,当您将clearerr(stdin)添加到此文件中时,它会有所帮助。

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

此外,由于在EOF之前不会停止初始输入,因此需要使用
clearr(stdin)
清除
stdin上的“error”(错误),以便进行进一步的输入。这在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;
}
使用clearerr(标准输入法)编码

输入要写入.c文件的数据:Happiness是一个没有bug的程序。
幸福难得。
EOF 1
但是,当您将clearerr(stdin)添加到此文件中时,它会有所帮助。
EOF 2
幸福是一个没有bug的程序。
幸福难得。
但是,当您将clearerr(stdin)添加到此文件中时,它会有所帮助。

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

此外,由于在EOF之前不会停止初始输入,因此需要使用
clearr(stdin)
清除
stdin上的“error”(错误),以便进行进一步的输入。这在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;
}
使用clearerr(标准输入法)编码

输入要写入.c文件的数据:Happiness是一个没有bug的程序。
幸福难得。
EOF 1
但是,当您将clearerr(stdin)添加到此文件中时,它会有所帮助。
EOF 2
幸福是一个没有bug的程序。
幸福难得。
但是,当您将clearerr(stdin)添加到此文件中时,它会有所帮助。

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

此外,由于在EOF之前不会停止初始输入,因此需要清除