程序从键盘读取数据,将其写入文件并再次从文件中读取相同的数据并将其复制到另一个文件并显示?

时间:2012-12-28 21:43:14

标签: c file-io input output

这是我到目前为止。我希望程序从键盘读取数据将其写入名为INPUT的文件,再次从INPUT文件中读取相同的数据,然后复制到该文件 另一个文件,并在屏幕上显示。 我不知道哪里出错了。 请帮我。 提前谢谢。

#include<stdio.h>
#include<file.h>
main()
{
  FILE *f1,*f2;
  char c;
  printf(“data input\n\n”)
  f1 = fopen(“INPUT”, “w”);
  while((c = getchar())!=EOF)
    putc(c,f1);
  fclose(f1);
  printf(“\ndata output\n\n”)
  f1 = fopen(“INPUT”, “r”);
  while((c = getchar())!=EOF)
    putc(“%c”,c);
  fclose(f1);
  //copying f f1 data into f2
  f1=fopen(file1,”r”)
  if(f1=NULL)
  {
    printf(“no data”);
    exit(0):
  }
  f2=fopen(file2,”w”);
  if(f2=null)
  {
    printf(“cannot able to open”);
    exit(0);
  }
  while((ch==getc(f1)!=EOF)
    putc(ch,f2);
  printf(“completed”);
  fclose(f1);
  fclose(f2);
}

1 个答案:

答案 0 :(得分:1)

您想要与NULL进行比较:

  if(f1=NULL)

但是这会分配NULL。

另一个比较中的相同问题:

  if(f2=null)

只需将其更改为:

if(!f1)

if(!f2)