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-
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;
}
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-
p->t = DBL_MAX;origin.x, p->origin.y, p->origin.z); */
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;
}