problem with file i/o in binary

Discussion in 'C Programming' started by pereges, Apr 28, 2008.

  1. pereges

    pereges Guest

    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;
    }
     
    pereges, Apr 28, 2008
    #1
    1. Advertising

  2. On Mon, 28 Apr 2008 01:01:18 -0700 (PDT), pereges <>
    wrote:

    >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-
    >>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)


    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
     
    Barry Schwarz, Apr 28, 2008
    #2
    1. Advertising

  3. pereges

    pereges Guest

    On Apr 28, 2:29 pm, Barry Schwarz <> wrote:

    > 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().
     
    pereges, Apr 28, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fangs
    Replies:
    3
    Views:
    9,947
    darshana
    Oct 26, 2008
  2. Marc Schellens
    Replies:
    8
    Views:
    3,068
    John Harrison
    Jul 15, 2003
  3. Ron Eggler

    writing binary file (ios::binary)

    Ron Eggler, Apr 25, 2008, in forum: C++
    Replies:
    9
    Views:
    969
    James Kanze
    Apr 28, 2008
  4. scad
    Replies:
    4
    Views:
    980
    James Kanze
    May 28, 2009
  5. Jim
    Replies:
    6
    Views:
    768
Loading...

Share This Page