Solved

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

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
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.

895 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

13 Experts available now in Live!

Get 1:1 Help Now