0
//Libraries are called accordingly 
struct std    
{
int a;
char b;
}// a structure with 2 objects

void main()

{

std h[5]; // structure array with 5 elements
int rec_size; // Size of single record to be accessed from the file 
rec_size=sizeof(structure std); // stores the size in bytes

while (true) 
  {
     fread(&h,rec_size,2,fptr); //fptr is a file pointer to some file

    /*Assume that the file pointer(fptr)has covered the last two record 
      blocks and stored
      them in "h". Then the shouldn't the position pointer proceed to end of 
      file and the next statement "if(feof(fptr)" then becomes true to exit 
      the loop and in essence miss out on the processing of those last two 
      record blocks?*/

    if(feof(fptr)) 
     /* Here, if the fptr has reached the end of file after fread(), 
        then this should execute*/                           
      break;

   //else additional code to process the content stored in "h" 
  }
}

My doubt is that when fread() function returns and in the next statement feof() checks for the EOF, shouldn't feof() return true if fread() processed the last two record blocks in the file? Could someone fully explain the functioning?

adits2
  • 11
  • 2

1 Answers1

1

feof() is used to know what kind of error you hit, not to detect one. The correct way is to check the valued return by fread() which will be 0 at the end of the file. Then if feof() is true, you know that you've reached EOF, otherwise there was an IO error.

So with your current code, you're processing twice the last readable records. Also, you mean &h[0] instead of &h.

Benoît
  • 3,355
  • 2
  • 29
  • 34