Hi guys, i'm having a little problem using the feof() function in Borland C++ for 3.1 for DOS, here's the code i'm trying to make work, it just store to long unsigned int number inside a file, well here it is:
Code:
FILE *arq_index;

// This function writes to the file
void insert(unsigned long int RA, unsigned long int OFFSET){
     arq_index=fopen("index.idx","ab+");
     fwrite(&RA,sizeof(RA),1,arq_index);
     fwrite(&OFFSET,sizeof(RA),1,arq_index);
     fclose(arq_index);
}

//This functions tries to read the file
void read(){
     unsigned long int RA=0, OFFSET=0;
     arq_index=fopen("index.idx","rb");
     if (arq_index){
          rewind(arq_index);
          clrscr();
          while (!feof(arq_index)){
               OFFSET=0;
               RA=0;
               fread(&RA,sizeof(RA),1,arq_index);
               fread(&OFFSET,sizeof(OFFSET),1,arq_index);
               printf("%s\n%s%ld\n%s%ld\n","------------------","RA : ",RA,OFFSET : ",OFFSET);
          }
          fclose(arq_index);
     }
}
Well, the problem is the following, let's say that I do the this :
Code:
void main(){
     for(int x=0; x < 2; x++)
          for(int y=0; y < 2; y++)
               insert(x,y);
}
this will put the following numbers into the file : [0,0];[0,1];[1,0];[1,1], until here, nothing special, more simples than this, impossible, but the problem comes when i'm reading the file, like this:

Code:
void main(){
/*
     for(int x=0; x < 2; x++)
          for(int y=0; y < 2; y++)
               insert(x,y);
}
*/
     read();
}
It prints out the following
Code:
------------------
RA : 0
OFFSET : 0
------------------
RA : 0
OFFSET : 1
------------------
RA : 1
OFFSET : 0
------------------
RA : 1
OFFSET : 1
------------------
RA : 0
OFFSET : 0
this last entry that's making me insane, why does it always print one more entry, no matter how many entries i put in it, it always prints out one more entry, so, i went debbuging the problem, i found that the problem is that the feof() function in :
while (!feof(arq_index)){ isn't finding the end of the file in its first attemp, like this, it reads the last register off the file, but, the feof() still lets the branch true, so it enters again, than the fread functions returns erros ( the zeros on the last entry) and then it comes back to the branch, only now it will be false... but why is that ?? i did an hexdump on the file, nothing wrong with its EOF char, any ideas ?

[ 19 April 2002: Message edited by: MaGneTTo ]