Solved

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

Posted on 2004-09-18
5
648 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files 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.

705 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