Fail to initialize struct from fstream

Discussion in 'C++' started by Kai Wu, Oct 9, 2004.

  1. Kai Wu

    Kai Wu Guest

    #include <string.h>
    #include <fstream>
    #include <time.h>

    typedef unsigned char BYTE;
    struct Dex {
    BYTE status;
    struct timeval timestamp;
    };
    int main(){
    ifstream in("FILE");
    BYTE buf[9];
    Dex d;
    in.read((char*)buf,9);
    while(in.good() && ! in.eof()){
    memcpy(&(d.status),buf,1);
    memcpy(&(d.timestamp),buf+1,8);
    in.read((char*)buf,9);
    }
    }

    Suppose FILE is a binary file with sequence of 9 bytes data, the 9 bytes
    match the structure defined by Dex,
    but above code fails to initialize the struct Dex as compiling with gcc,
    with HP aCC it works.

    is there something wrong with the code (or with gcc)? or is there a
    better way to initialize the struct?
    Thanks for your time.
     
    Kai Wu, Oct 9, 2004
    #1
    1. Advertising

  2. "Kai Wu" <> wrote in message
    news:SqO9d.27307$...
    > #include <string.h>
    > #include <fstream>
    > #include <time.h>
    >
    > typedef unsigned char BYTE;
    > struct Dex {
    > BYTE status;
    > struct timeval timestamp;
    > };
    > int main(){
    > ifstream in("FILE");
    > BYTE buf[9];
    > Dex d;
    > in.read((char*)buf,9);
    > while(in.good() && ! in.eof()){
    > memcpy(&(d.status),buf,1);
    > memcpy(&(d.timestamp),buf+1,8);
    > in.read((char*)buf,9);
    > }
    > }
    >
    > Suppose FILE is a binary file with sequence of 9 bytes data, the 9 bytes
    > match the structure defined by Dex,
    > but above code fails to initialize the struct Dex as compiling with gcc,
    > with HP aCC it works.
    >
    > is there something wrong with the code (or with gcc)? or is there a
    > better way to initialize the struct?
    > Thanks for your time.


    Why are you repeatedly initialising the same struct? Why a while loop in
    other words.

    In any case this is a better way, whether this fixes your problem or not I
    can't say, since you haven't provided any clues as to what actually went
    wrong.

    typedef unsigned char BYTE;
    struct Dex {
    BYTE status;
    struct timeval timestamp;
    };

    int main(){
    ifstream in("FILE", ios_base::binary); // its a binary file
    char buf[9]; // no need for BYTEs
    Dex d;
    while (in.read(buf,9)) { // this is the simple way to tell if a read
    has succeeded or not
    d.status = buf[0];
    memcpy(&d.timestamp,buf + 1,8);
    }
    }

    Binary reads are never very portable, possibly that is where you problem is.
    If the code above doesn't help then describe a bit more about what actually
    goes wrong, and how the binary file came to be written in the first place.

    john
     
    John Harrison, Oct 9, 2004
    #2
    1. Advertising

  3. Kai Wu

    Kai Wu Guest

    Thank you so much john,
    actually there is vector collecting the Dex in the while loop ... but it
    is missed, my fault.
    while the read part is indeed a lot cleaner, but struct initialize still
    fails,
    guess you must be right, as the original binary file is written in HP.

    John Harrison wrote:
    > "Kai Wu" <> wrote in message
    > news:SqO9d.27307$...
    >
    >>#include <string.h>
    >>#include <fstream>
    >>#include <time.h>
    >>
    >>typedef unsigned char BYTE;
    >>struct Dex {
    >> BYTE status;
    >> struct timeval timestamp;
    >>};
    >>int main(){
    >> ifstream in("FILE");
    >> BYTE buf[9];
    >> Dex d;
    >> in.read((char*)buf,9);
    >> while(in.good() && ! in.eof()){
    >> memcpy(&(d.status),buf,1);
    >> memcpy(&(d.timestamp),buf+1,8);
    >> in.read((char*)buf,9);
    >> }
    >>}
    >>
    >>Suppose FILE is a binary file with sequence of 9 bytes data, the 9 bytes
    >>match the structure defined by Dex,
    >>but above code fails to initialize the struct Dex as compiling with gcc,
    >>with HP aCC it works.
    >>
    >>is there something wrong with the code (or with gcc)? or is there a
    >>better way to initialize the struct?
    >>Thanks for your time.

    >
    >
    > Why are you repeatedly initialising the same struct? Why a while loop in
    > other words.
    >
    > In any case this is a better way, whether this fixes your problem or not I
    > can't say, since you haven't provided any clues as to what actually went
    > wrong.
    >
    > typedef unsigned char BYTE;
    > struct Dex {
    > BYTE status;
    > struct timeval timestamp;
    > };
    >
    > int main(){
    > ifstream in("FILE", ios_base::binary); // its a binary file
    > char buf[9]; // no need for BYTEs
    > Dex d;
    > while (in.read(buf,9)) { // this is the simple way to tell if a read
    > has succeeded or not
    > d.status = buf[0];
    > memcpy(&d.timestamp,buf + 1,8);
    > }
    > }
    >
    > Binary reads are never very portable, possibly that is where you problem is.
    > If the code above doesn't help then describe a bit more about what actually
    > goes wrong, and how the binary file came to be written in the first place.
    >
    > john
    >
    >
     
    Kai Wu, Oct 9, 2004
    #3
  4. "Kai Wu" <> wrote in message
    news:l8P9d.27378$...
    > Thank you so much john,
    > actually there is vector collecting the Dex in the while loop ... but it
    > is missed, my fault.
    > while the read part is indeed a lot cleaner, but struct initialize still
    > fails,
    > guess you must be right, as the original binary file is written in HP.
    >


    The most likely thing wrong is that the bytes in each integer are in the
    wrong order. Try swapping the bytes around. Something like this

    #include <algorithm> // for std::swap

    int main(){
    ifstream in("FILE", ios_base::binary); // its a binary file
    char buf[9]; // no need for BYTEs
    Dex d;
    while (in.read(buf,9)) {
    d.status = buf[0];
    std::swap(buf[1], buf[4]);
    std::swap(buf[2], buf[3]);
    std::swap(buf[5], buf[8]);
    std::swap(buf[6], buf[6]);
    memcpy(&d.timestamp,buf + 1,8);
    }
    }

    john
     
    John Harrison, Oct 9, 2004
    #4
  5. > std::swap(buf[6], buf[6]);

    Typo

    std::swap(buf[6], buf[7]);

    john
     
    John Harrison, Oct 9, 2004
    #5
  6. "Kai Wu" <> wrote in message news:SqO9d.27307$...
    > #include <string.h>
    > #include <fstream>
    > #include <time.h>
    >
    > typedef unsigned char BYTE;
    > struct Dex {
    > BYTE status;
    > struct timeval timestamp;
    > };
    > int main(){

    #if 0
    > ifstream in("FILE");

    #endif
    std::ifstream in("FILE", std::ios_base::binary);

    > BYTE buf[9];
    > Dex d;
    > in.read((char*)buf,9);
    > while(in.good() && ! in.eof()){
    > memcpy(&(d.status),buf,1);
    > memcpy(&(d.timestamp),buf+1,8);
    > in.read((char*)buf,9);
    > }
    > }
    >
    > Suppose FILE is a binary file with sequence of 9 bytes data, the 9 bytes match the structure defined by Dex,
    > but above code fails to initialize the struct Dex as compiling with gcc, with HP aCC it works.


    Is it possible you have done these 2 trials on different
    platforms? (such as a Windows and Unix-like machine)

    > is there something wrong with the code (or with gcc)?


    I doubt that you have exposed a gcc flaw here.

    Please study the above correction to your code, lookup
    the meaning of the additional ctor argument, then see if
    your difficulty does not become more apparent.

    > or is there a better way to initialize the struct?


    Binary structure I/O is notoriously unportable. If you are
    a student, I would urge you to not make that a habit.

    > Thanks for your time.

    You're welcome.

    --
    --Larry Brasfield
    email:
    Above views may belong only to me.
     
    Larry Brasfield, Oct 9, 2004
    #6
  7. Kai Wu

    Kai Wu Guest

    Gee... u r right John. the byte order needs to be flipped. THANKS SO
    MUCH :)
    maybe i need to refine a little bit further .... wrap the swap in a
    routine, as there are another
    couple of IO, which has more complicated struct.

    BTW, the code will eventually compiled under HP, it just feels good
    under linux and do the test.

    John Harrison wrote:
    > "Kai Wu" <> wrote in message
    > news:l8P9d.27378$...
    >
    >>Thank you so much john,
    >>actually there is vector collecting the Dex in the while loop ... but it
    >>is missed, my fault.
    >>while the read part is indeed a lot cleaner, but struct initialize still
    >>fails,
    >>guess you must be right, as the original binary file is written in HP.
    >>

    >
    >
    > The most likely thing wrong is that the bytes in each integer are in the
    > wrong order. Try swapping the bytes around. Something like this
    >
    > #include <algorithm> // for std::swap
    >
    > int main(){
    > ifstream in("FILE", ios_base::binary); // its a binary file
    > char buf[9]; // no need for BYTEs
    > Dex d;
    > while (in.read(buf,9)) {
    > d.status = buf[0];
    > std::swap(buf[1], buf[4]);
    > std::swap(buf[2], buf[3]);
    > std::swap(buf[5], buf[8]);
    > std::swap(buf[6], buf[6]);
    > memcpy(&d.timestamp,buf + 1,8);
    > }
    > }
    >
    > john
    >
    >
     
    Kai Wu, Oct 10, 2004
    #7
    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. Armando
    Replies:
    6
    Views:
    771
    Armando
    Jan 29, 2004
  2. Chris Fogelklou
    Replies:
    36
    Views:
    1,422
    Chris Fogelklou
    Apr 20, 2004
  3. Replies:
    14
    Views:
    1,099
    Jerry Coffin
    Jul 3, 2006
  4. Ehud Shapira
    Replies:
    20
    Views:
    1,163
    Ehud Shapira
    Jun 30, 2007
  5. Disc Magnet
    Replies:
    3
    Views:
    1,228
    James Kanze
    Mar 5, 2010
Loading...

Share This Page