Fail to initialize struct from fstream

K

Kai Wu

#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.
 
J

John Harrison

Kai Wu said:
#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
 
K

Kai Wu

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 said:
#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
 
J

John Harrison

Kai Wu said:
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
 
L

Larry Brasfield

Kai Wu said:
#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.
 
K

Kai Wu

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 said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top