problem with file i/o in binary

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pereges

    problem with file i/o in binary

    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.r ay_spacing is a double and value is 0.05 */

    ray *p;

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

    p = malloc(sizeof(r ay));
    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.E 0;
    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("\nOrigi n: %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;
    }
  • pereges

    #2
    Re: problem with file i/o in binary


    On Apr 28, 2:29 pm, Barry Schwarz <schwa...@dqel. comwrote:
    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().

    Comment

    Working...