1

I have a function that should read a file and detect when the file ends.

The function currently reads only the second last line and ends. Can someone please tell me what I'm doing wrong as I can't see it myself:

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;
  }
Dawson
  • 573
  • 6
  • 24
  • how do you expect it would detect if the file was ended? You do a finit series of fscanf calls based on `incan->ac-1`, then one more, then call feof. How would that find the end of the file? It will tell you if you're at the end, but it won't somehow read data until eof, will it? Maybe I'm missing something. – xaxxon May 19 '13 at 08:14
  • why don't you give us the smallest input file that reproduces this file and what output you get from this program on that output vs what you expect? – xaxxon May 19 '13 at 08:28
  • Do you have `'\n'` on last line? So you should have last line as empty line. Otherwise `fscanf("...\n");` will not put the values in variables appropriately. – Rohan May 19 '13 at 08:31
  • Please don't double-post. – likeitlikeit May 20 '13 at 00:02

1 Answers1

1

I would expect your program to have a loop in it similar to:

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

if you want to detect when the file ends.

xaxxon
  • 19,189
  • 5
  • 50
  • 80
  • That would work for scanning until the end of the file, but I want to read one line at a time as specified by the ac value and then take actions if that line read was the last. – Dawson May 19 '13 at 08:19
  • 1
    So how do you know it's one line too early? – xaxxon May 19 '13 at 08:20
  • The ac is the line i want to read, so i skip AC-1 and then read the correct line. – Dawson May 19 '13 at 08:21
  • 1
    that's fine, but how do you know it's testing eof == true one line too early? – xaxxon May 19 '13 at 08:22
  • I've tested the program and the last line is never read. – Dawson May 19 '13 at 08:24
  • 1
    define never read? It's obviously read, or you wouldn't get EOF, so it's getting lost somehow. You should also print out the return value from all the fscanf calls and print those out. – xaxxon May 19 '13 at 08:27
  • OK, well let me know what you figure out. I'm curious. – xaxxon May 19 '13 at 08:33
  • I think the problem is that feof is detecting false ends and increasing the terminated value more than it should. My program loops while terminated is less than a certain value so some lines never get read as the program finishes too soon. – Dawson May 19 '13 at 08:38
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/30201/discussion-between-dawson-and-xaxxon) – Dawson May 19 '13 at 08:41