c - feof() 在实际结束前检测到文件末尾一行

标签 c scanf feof

我有一个函数应该读取文件并检测文件何时结束。

该函数当前仅读取倒数第二行并结束。有人可以告诉我我做错了什么吗,因为我自己看不到:

int readIn(TinCan* inCan, int toggle)
  {
  int ii, isFinished = 0;
  char fullName[20];
  sprintf(fullName, "Label_%d.txt", inCan->pid);

  FILE* fp; 
    fp = fopen(fullName, "r");

    if(fp==NULL) 
      {
      printf("Error: could not open %s\n", fullName);
      }

    else
      {
      for (ii=0; ii < ((inCan->ac)-1); ii++)
        {
        fscanf(fp, "%*d %*d %*d\n"); /*move through lines without scanning*/
        }
      fscanf(fp,"%d %d %d", &inCan->ac, &inCan->state, &inCan->time);
      }

    if (feof(fp) && (toggle == 1)) 
      {
      printf("File ended"); 
      writeLog(inCan);
      isFinished = 1;
      terminated++;
      }

  fclose(fp);
  return finished;
  }

最佳答案

我希望你的程序中有一个类似于以下内容的循环:

while (!feof(fp)) {
...
    fscanf(fp, "%*d %*d %*d\n");
...
}

如果你想检测文件何时结束。

关于c - feof() 在实际结束前检测到文件末尾一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16632619/

相关文章:

条件检查 scanf () 或获取输入

c - 当我不知道它会在 C 中分配多少个值时,如何使用 scanf?

c - scanf 和 "\n"转义序列

C - 在阅读之前找到文件结尾

C - 在使用 sprintf 和打印字符串时遇到问题

java - windows下socket读取超时: strange hardcode in native method

java - 滚动 Pane 如何滚动

c - 分代GC源代码

c - 为什么“while(!feof(file))”总是错误的?