C语言判断文件为空问题

RT.if(feof(fp){
printf("文件内容为空,请先输入,按任意键返回");
system("pause");}这样为什么不能判断是否为空?

在“C”文件读取操作时不能完全依赖于“while(!feof(FP)){...}”的判断。下面代码是改进后的代码,该代码执行后output文件内容和input文件内容一致,与使用“while(!feof(FP)){...}”相比,input文件的结尾符号(EOF)没有被读入到output文件中。
  //main.c linux 下编译通过。
  int main(void)
  {
  FILE *in, *out;
  char ch;
  if ((in = fopen("./input.txt", "r"))== NULL) //input.txt must exist in current directory.
  {
  fprintf(stderr, "Cannot open inputfile\n");
  exit(0);
  }
  if((out=fopen("./output.txt","w"))==NULL)
  {
  fprintf(stderr,"Can not open the file.\n");
  exit(0);
  }
  while(1)
  {
  ch=fgetc(in);
  if(ch == -1)
  break;
  fprintf(stdout,"The ASC of char %c is %d\n ",ch,ch);
  fputc(ch,out);
  }
  fclose(in);
  fclose(out);
  return 0;
  }
温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答