Solved

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

Posted on 2004-09-18
5
630 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
memory leak detection 9 51
Problem in finding output of a program 11 93
Passing a array as parameter - C 2 77
C Language combined operators 28 101
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

743 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

14 Experts available now in Live!

Get 1:1 Help Now