Solved

while(!feof(filePtr)) loops record twice ?

Posted on 2004-09-18
5
636 Views
Last Modified: 2008-03-06
Hi, I'm trying to iterate and display all records in a file. I currently only have ONE record in the file but when I do this.

      FILE *cfPtr;
      struct errorData error = {0,"",""};

      if((cfPtr=fopen("database.dat","rb")) == NULL) {
            printf("File could not be opened.\n");
      }
      else {
            while(!feof(cfPtr)) {
                  fread(&error,sizeof(struct errorData),1,cfPtr);

                  if(error.errorNo != 0) {
                        printf("%d\n",error.errorNo);
                        fflush(stdout);
                        puts(error.description);
                        puts(error.solution);
                  }
            }
            fclose(cfPtr);
      }

that record displays twice:
record1
description1
solution1
record1
description1
solution1

what is wrong??
0
Comment
Question by:lewin85_sg
  • 4
5 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12092144
The problem is that feof() is not detecting end of file correctly. I suppose it's due the "rb" attribute you specified in fopen(), try to use "rt" instead.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12092155
Some additional explanation:
Text files use to have an extra "EOF" character at the end (also know as Ctrl-Z). If you open file in "text" mode (rt) then this character will be considered as end-of-file marker and not as another data character. If you open in "binary" mode (rb) then this character will be considered a normal character and feof() will return false one more time, until next fread() will bring you to the physical end-of-file.
Hope to be clear.
Jaime.
0
 
LVL 1

Author Comment

by:lewin85_sg
ID: 12092252
hi, i replaced rb with rt but it still display twice. i added another record to the file and noticed that only the last record in the file is repeated twice.
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 250 total points
ID: 12092265
replace your code with:
if((cfPtr=fopen("database.dat","rb")) == NULL) {
          printf("File could not be opened.\n");
     }
     else {
          while(!feof(cfPtr)) {
               if (fread(&error,sizeof(struct errorData),1,cfPtr)==sizeof(struct errorData)) {
                    printf("%d\n",error.errorNo);
                    fflush(stdout);
                    puts(error.description);
                    puts(error.solution);
               }
          }
          fclose(cfPtr);
     }

0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12092358
I made a mistake considering your file was a text file. In fact, it is binary since it contains a binary value in the structure, so it is correct to open as "rb". About the last version, fread() returns number of characters read, so, if it is not as expected, then you have reached end-of-file.
Thanks for the point. Good luck,
Jaime.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now