繁体   English   中英

fgetc始终返回EOF

[英]fgetc Always returns EOF

在尝试计算文本文件中的行数时,我注意到fgetc始终返回EOF。 该代码在Freebsd 10上可用,但现在在Mac OSX上不可用。 我检查了文件是否为空,不是,它的大小约为1 KB,包含16行。 我在该文件的开头添加了一行以查找问题所在,但它仍在返回EOF。 那么,为什么fgetc总是返回EOF?

 int getLines(int listFd, int *lines)
 {

     /* Declarations */
     *lines = 0;
     int ch;
     FILE *list;

     /* Get File Stream */
     list = fdopen(listFd, "r");
     if(list == NULL)
     {
         printf("Can't Open File stream\n");
         return -1;
     }

     /* Seek To beginning Of file */
     fseek(list, 0, SEEK_SET);

     /* Get Number of Lines */
     while(!feof(list))
     {
         ch = fgetc(list);
         if(ch == '\n')
         {
             lines++;
         }

         else if(ch == EOF)
         {
             break;
         }
     }

     printf("lines: %d\n", *lines);

     /* Clean up and Exit */
     fclose(list);

    return 0;
}

fgetc()最终返回EOF 代码的另一个问题当然是混淆行为和诊断。 很好的测试IO功能的结果。

int getLines(int listFd, int *lines) {
  ...
  *lines = 0;
  ...
  if (fseek(list, 0, SEEK_SET)) puts("Seek error");
  ...
    ch = fgetc(list);
    if (ch == '\n') {
      // lines++;
      (*lines)++;  // @R Sahu 
    }
   ...
   printf("lines: %d\n", *lines);

   if (ferror(list)) puts("File Error - unexpected");
   if (feof(list)) puts("File EOF - expected");
}

其他的东西:

以下代码是文件结束条件的冗余测试

/* Get Number of Lines */
while(!feof(list)) {
  ch = fgetc(list);
  ...
  else if(ch == EOF) {
    break;
  }
}

建议简化(@Keith Thompson)

 /* Get Number of Lines */
 while( (ch = fgetc(list)) != EOF) {
   if(ch == '\n') {
     (*lines)++;
   }
 }

关于文件行数的要点:如果文件的末尾'\\n' 之后有文本,那算作行吗? 建议:

*lines = 0;
int prev = '\n'; 
/* Get Number of Lines */
while( (ch = fgetc(list)) != EOF) {
  if(prev == '\n') {
    (*lines)++;
  }
  prev = ch;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM