problem with file i/o in binary

P

pereges

I'm having a problem with reading a file that was written in binary.
This is the main structure of the data i want to write:


typedef struct ray_struct
{
double t;
vector origin; /* vector is a struct with 3 doubles x,y , z */
vector d;
int depth;
double efield_x, efield_y;

}ray;

I have two functions, compute_planar_source() writes the data and
fire_rays() reads the data:

int compute_planar_source()
{
...................

FILE *fp;
fp = fopen("rayinf", "wb");

if(fp == NULL)
{
perror("File could not be opened!");
return FAILURE
}

/* i have left out data structures for minB, maxB and radardetector
but the values are as follows:
minB.y = minB.x = minB.z = -10 a double
maxB.y = maxB.y = maxB.z= 10 a double

xcord and ycord are doubles
radardetector.ray_spacing is a double and value is 0.05 */

ray *p;

for(ycord = minB.y; ycord<= maxB.y; ycord +=
radardetector.ray_spacing )
{
for(xcord = minB.x; xcord<=maxB.x; xcord +=
radardetector.ray_spacing)
{

p = malloc(sizeof(ray));
if(p == NULL)
{
perror("malloc has failed!");
return FAILURE;
}
p->origin.x = xcord;
p->origin.y = ycord;
p->origin.z = minB.z - 1000;
/* this prints fine : printf("%f %f %f\n", p-
origin.x, p->origin.y, p->origin.z); */
p->t = DBL_MAX;
p->d.x = 0;
p->d.y = 0;
p->d.z = 1;
p->depth = 0;
p->efield_x = radardetector.E0;
p->efield_y = 0;
fwrite(p, sizeof(ray), 1, fp);
free(p);
}

}

fclose(fp);

}

int fire_rays()
{
FILE *fp;
ray r;

fp = fopen("rayinf", "rb");

if(fp == NULL)
{
perror("File open failed");
return FAILURE;
}

while(fread(&r, 1, sizeof(ray), fp) == 1)
{
printf("\nOrigin: %f %f %f\n", r.origin.x, r.origin.y,
r.origin.z); // Doesn't print at all ??
}

}


int main()
{

int i;

i = compute_planar_source();
if(i == FAILURE)
{
printf("compute_planar_source() has failed\n");
}

i = fire_rays();
if(i == FAILURE)
{
printf(" fire_rays() has failed\n");
}

return 0;
}
 
B

Barry Schwarz

I'm having a problem with reading a file that was written in binary.

It would be nice if you provided a description of your problem as part
of the discussion and not as a miniscule comment buried in the code.
This is the main structure of the data i want to write:

If you look at your fwrite and your fread statements carefully, the
difference in the way you coded them should give you enough of a hint
to solve the problem. Otherwise, read my comment at the fread
statement.
typedef struct ray_struct
{
double t;
vector origin; /* vector is a struct with 3 doubles x,y , z */
vector d;
int depth;
double efield_x, efield_y;

}ray;

I have two functions, compute_planar_source() writes the data and
fire_rays() reads the data:

int compute_planar_source()
{
...................

FILE *fp;
fp = fopen("rayinf", "wb");

if(fp == NULL)
{
perror("File could not be opened!");
return FAILURE
}

/* i have left out data structures for minB, maxB and radardetector
but the values are as follows:
minB.y = minB.x = minB.z = -10 a double
maxB.y = maxB.y = maxB.z= 10 a double

xcord and ycord are doubles
radardetector.ray_spacing is a double and value is 0.05 */

ray *p;

for(ycord = minB.y; ycord<= maxB.y; ycord +=
radardetector.ray_spacing )
{
for(xcord = minB.x; xcord<=maxB.x; xcord +=
radardetector.ray_spacing)
{

p = malloc(sizeof(ray));
if(p == NULL)
{
perror("malloc has failed!");
return FAILURE;
}
p->origin.x = xcord;
p->origin.y = ycord;
p->origin.z = minB.z - 1000;
/* this prints fine : printf("%f %f %f\n", p-
p->t = DBL_MAX;
p->d.x = 0;
p->d.y = 0;
p->d.z = 1;
p->depth = 0;
p->efield_x = radardetector.E0;
p->efield_y = 0;
fwrite(p, sizeof(ray), 1, fp);
free(p);
}

}

fclose(fp);

}

int fire_rays()
{
FILE *fp;
ray r;

fp = fopen("rayinf", "rb");

if(fp == NULL)
{
perror("File open failed");
return FAILURE;
}

while(fread(&r, 1, sizeof(ray), fp) == 1)

You ask fread to read several objects, each of size 1. If fread ever
did return a 1, it would mean it read a single byte. Either provide a
better comparand (my preference) or re-order your second and third
arguments.
..
{
printf("\nOrigin: %f %f %f\n", r.origin.x, r.origin.y,
r.origin.z); // Doesn't print at all ??
}

}


int main()
{

int i;

i = compute_planar_source();
if(i == FAILURE)
{
printf("compute_planar_source() has failed\n");
}

i = fire_rays();
if(i == FAILURE)
{
printf(" fire_rays() has failed\n");
}

return 0;
}


Remove del for email
 
P

pereges

It would be nice if you provided a description of your problem as part
of the discussion and not as a miniscule comment buried in the code

Well the problem was related to ray tracing. I was trying to generate
the rays and store them them on a file. The write operation was
working fine but there was a problem in reading the ray information
from the file.


If you look at your fwrite and your fread statements carefully, the
difference in the way you coded them should give you enough of a hint
to solve the problem. Otherwise, read my comment at the fread
statement.

Thanks for your help. I realize the problem was in fread().
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top