Solved

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

Posted on 2004-09-18
5
639 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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

807 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