Re: Fail to initialize struct from fstream
From: John Harrison (john_andronicus_at_hotmail.com)
Date: 10/09/04
- Next message: John Harrison: "Re: is this portable"
- Previous message: JKop: "Re: accessing variables from more than one .cpp file"
- Maybe in reply to: John Harrison: "Re: Fail to initialize struct from fstream"
- Next in thread: John Harrison: "Re: Fail to initialize struct from fstream"
- Reply: John Harrison: "Re: Fail to initialize struct from fstream"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 9 Oct 2004 12:45:47 +0100
"Kai Wu" <kai_wu2004@fastmail.cn> wrote in message
news:l8P9d.27378$g4.512853@news2.nokia.com...
> 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
- Next message: John Harrison: "Re: is this portable"
- Previous message: JKop: "Re: accessing variables from more than one .cpp file"
- Maybe in reply to: John Harrison: "Re: Fail to initialize struct from fstream"
- Next in thread: John Harrison: "Re: Fail to initialize struct from fstream"
- Reply: John Harrison: "Re: Fail to initialize struct from fstream"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|