fread seems to be a byte illiterate

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JoMomma
    New Member
    • Mar 2008
    • 2

    fread seems to be a byte illiterate

    i've been given a binary file to open and donot know what it contains except that it has floats, ints and char[x] in it. I've built a struct to hold the data. just do not know how to read it. The first and second time through the loop works good. Get funcky stuff after that.

    #include "stdafx.h"
    #include <iostream>
    using namespace System;
    using namespace std;

    #using <mscorlib.dll >

    using namespace System;

    struct record
    {

    int s_Nums;
    char s_Desc[25];
    float s_Cost;
    float s_Time;
    int s_Made;
    int s_Part;

    };

    void main()
    {

    FILE *f_Ptr;

    struct record s_Test;

    f_Ptr = fopen("V.DAT"," rb");

    for (int x = 0; x <= 5; x++)
    {

    fread(&s_Test, sizeof(struct record), 1, f_Ptr);

    printf("\n\t %i ", s_Test.s_Nums);
    printf("\n\t %s ", s_Test.s_Desc);
    printf("\n\t %.2f ", s_Test.s_Cost);
    printf("\n\t %.2f ", s_Test.s_Time);
    printf("\n\t %i ", s_Test.s_Made);
    printf("\n\t %i \n", s_Test.s_Part);

    printf("\n\t\n" );

    }// End of for

    fclose(f_Ptr);

    }// End of main




    this is the file data inside of the binary file called "V.DAT"




     compressor ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ ÌÌR¸†@33“AÎ  y
    housing ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ ÌÌÌÌÌq=A 9 piston ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ ÌÌÌÌÌ)\©A J
    gear n ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ ÌÌÌÌ̸?  generator ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ ÌÌìQTAÍÌ´Av  |
    housing ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ ÌÌÌÌÌq=A  clamp g ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ ÌÌÌÌÌÃõh? ë pad p g ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ ÌÌÌÌÌ…ë> à coil g ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ ÌÌÌÌÌ×£Ð@ J
    gear g ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ ÌÌÌÌ̸? f gear g ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ ÌÌÌÌÌHá:? " mixer tor ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ ÌÌ33AÍÌ”A?  
    housing ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ ÌÌÌÌÌ®G5A  clamp g ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ ÌÌÌÌÌÃõh? ú moter g ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ ÌÌÌÌÌ…ë£A à coil g ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ ÌÌÌÌÌ×£Ð@ J
    gear g ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ ÌÌÌÌ̸? f gear g ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ ÌÌÌÌÌHá:? ' flashlight ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ ÌÌ ffF@  
    housing ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ ÌÌÌÌÌ
    ׃? Š bulb ng ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ ÌÌÌÌÌ®Gá=  lense g ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ ÌÌÌÌÌ=
    W> 2 grinder ht ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ ÌÌ 333?  Â morter ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ ÌÌÌÌÌq=Ê? Ê pestle ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ ÌÌÌÌÌHá
    @ : radio r ht ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ ÌÌìQ˜?ff–@4  ³ capacitor ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ ÌÌÌÃõ¨? Ë resister ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ ÌÌ̏•? Í resister ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ ÌÌÌq=ê? Ñ resister ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ ÌÌÌR¸ž? r speaker ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ ÌÌÌ=
    §@ ö push button ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ Ì¸>
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    It looks to me like the strings are not all the same length and are certainly not 25 characters long in which case you can not read the data into a structure like that.

    Anyway reading a file like that is not a good idea because it is not portable, on a different computer with different structure packing rules or different size integers or different endianness of integers you file read routine will fail.

    The best thing is to read the individual bytes of data from the file and use the to reconstruct your data type

    For instance reading a (32 bit) integer do not do this

    [code=c]
    int i;

    fread ( &i, sizeof i, 1, fin);
    [/code]

    do this

    [code=c]
    int i;
    unsigned char data[4];

    fread ( data, 1, sizeof data, fin);

    i = (int)(((unsigne d)data[0] << 24)
    | ((unsigned)data[1] << 16)
    | ((unsigned)data[2] << 8)
    | (unsigned)data[3]);
    [/code]

    Then all you have to worry about is if the platform that wrote the file has the same number of bits in the byte as the platform you are using to read it.

    Comment

    • JoMomma
      New Member
      • Mar 2008
      • 2

      #3
      thank you for replying. I needed a direction to go into and you have given it to me. thank you.

      Comment

      Working...