I am using tmpfile()
to create a file (so it's automatically opened in binary mode).
Then I write some floats
in it with fwrite()
. All these floats
are > 0.
The problem is that when I try to read these values (that are >0) back with fread()
, I get negative values!
The values I write are out[i][j][couche]
which are OK (it is impossible for them to be <0 because of their definition). The problem is at the end (fread
).
void flou_bis (FILE * fp, PIXRVB **out, PIXRVB **in, int np, int nl, int rayon, int couche){
int i,j,k,l,nb;
float ret1, ret2;
float rCouche;
/* Other things not relevant..
(process the values of array out[][][] ...)*/
for (i=0; i<nl; i++)
{
for (j=0; j<np; j++)
{
fwrite ( &(out[i][j][couche]) , sizeof(float), 1, fp);
}
}
rewind(fp);
/*fsync(fp); // Useless
rewind(fp);*/
printf("%d float read\n", fread ( &ret2, sizeof(float), 1, fp)); /*Here is the problem!!! */
printf("%f\n", ret2);
}
Problem solved, I needed to create a temp variable to force the cast:
float ent = (float) out[i][j][couche];
fwrite ( &ent, sizeof(float), 1, fp);
without the (float) it doesn't work, I have no idea why Thank you all for helping ;)