Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Fail to initialize struct from fstream

Reply
Thread Tools

Fail to initialize struct from fstream

 
 
Kai Wu
Guest
Posts: n/a
 
      10-09-2004
#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,;
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.
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      10-09-2004

"Kai Wu" <(E-Mail Removed)> wrote in message
news:SqO9d.27307$(E-Mail Removed)...
> #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,;
> 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,;
}
}

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


 
Reply With Quote
 
 
 
 
Kai Wu
Guest
Posts: n/a
 
      10-09-2004
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" <(E-Mail Removed)> wrote in message
> news:SqO9d.27307$(E-Mail Removed)...
>
>>#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,;
>> 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,;
> }
> }
>
> 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
>
>

 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      10-09-2004

"Kai Wu" <(E-Mail Removed)> wrote in message
news:l8P9d.27378$(E-Mail Removed)...
> 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,;
}
}

john


 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      10-09-2004
> std::swap(buf[6], buf[6]);

Typo

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

john


 
Reply With Quote
 
Larry Brasfield
Guest
Posts: n/a
 
      10-09-2004
"Kai Wu" <(E-Mail Removed)> wrote in message news:SqO9d.27307$(E-Mail Removed)...
> #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,;
> 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: http://www.velocityreviews.com/forums/(E-Mail Removed)
Above views may belong only to me.


 
Reply With Quote
 
Kai Wu
Guest
Posts: n/a
 
      10-10-2004

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" <(E-Mail Removed)> wrote in message
> news:l8P9d.27378$(E-Mail Removed)...
>
>>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,;
> }
> }
>
> john
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
Initialize pointer-to-struct declaration to an unnamed struct? Ehud Shapira C++ 20 06-30-2007 04:10 PM
initialize a string or a stringstream with fstream cpisz@austin.rr.com C++ 14 07-03-2006 04:14 AM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM
what is different between <fstream.h> and <fstream>MS VC++ Armando C++ 6 01-29-2004 09:01 AM



Advertisments