int ricerca_copia(int x,int xf)
{
FILE *xp;
int conf;
int f;
int xeof;
f=0;
// file was already written and closed before this function
xp=fopen("prenotazioni_copy.dat","rb");
if(xp)
{
xeof=0;
while(xeof==0)
{
//Never changes xp value
fread(&appoggio,sizeof(appoggio),1,xp);
if(feof(xp))
{
//never enters here
xeof=1;
}
else
{
//other stuff
}
fclose(xp);
}
}
printf("\nValore %d",f);
return f;
}
fread doesn't change pointer (xp) value,so the xeof value remains 0 and the loop restarts
I have tested already to use this part of code externally of this function and it works,so the problem is this.
I don't really know how to solve,so there are any solution?
the problem is that you call fclose(xp)
in your loop.
Do first iteration works okay, but next fread
call fails (you should check what fread
returns BTW, it's supposed to return the number of elements read, so if it doesn't and returns 0 or -1 there's a problem), and so does feof
on that handle (well, actually it's undefined behaviour as stated here: What is the return value of feof() on a closed file?)
You mean to call fclose(xp)
outside the loop or you get an infinite loop (unless the file is smaller that the size of the struct, which is another functional error)
Aside: xp
is a pointer on FILE
opaque structure. It doesn't change even with multiple fread
. Which changes is some hidden offset
value. But you cannot get that information unless you call ftell
on the handle.