473,326 Members | 2,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Fail to initialize struct from fstream

#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.
Jul 22 '05 #1
6 2719

"Kai Wu" <ka********@fastmail.cn> wrote in message
news:Sq*******************@news1.nokia.com...
#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
Jul 22 '05 #2
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" <ka********@fastmail.cn> wrote in message
news:Sq*******************@news1.nokia.com...
#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

Jul 22 '05 #3

"Kai Wu" <ka********@fastmail.cn> wrote in message
news:l8*******************@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
Jul 22 '05 #4
> std::swap(buf[6], buf[6]);

Typo

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

john
Jul 22 '05 #5
"Kai Wu" <ka********@fastmail.cn> wrote in message news:Sq*******************@news1.nokia.com...
#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: do***********************@hotmail.com
Above views may belong only to me.
Jul 22 '05 #6

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" <ka********@fastmail.cn> wrote in message
news:l8*******************@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

Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: m vaughn | last post by:
I wanted to hold information about open files in a struct and be able pass it around, but the behavior is not what I would expect. Can someone tell me why this doesn't work? (Even better, what is...
18
by: ineedyourluvin1 | last post by:
Hi, I would appreciate if someone could tell me what I'm doing wrong ? #include<iostream> using namepace std ; struct person{ char *firstname ; int age ;
14
by: cpisz | last post by:
I want to do some find and erase operations that i find in the string class, upon the entire text contents of a file. I made a function that will take a string (designed to hold the entire file...
10
by: wenmang | last post by:
hi, I have following: struct array1 { int id; char *name; }; struct array2 {
18
by: cmk128 | last post by:
hi 1 #include <stdio.h> 2 3 class A{ 4 private: 5 int x; 6 public: 7 A(int x){ 8 this->x;
6
by: paul.anderson | last post by:
This code doesn't work - the first retrieval of t2 returns valid data, the subsequent do not. Please help!!! int main(int argc, char* argc){ struct Test{ int i; int j; intk; int l; }
3
by: Bob Altman | last post by:
Hi all, If I have a class that includes an instance of a struct as a member, how do I initialize that struct? I can't find a syntax for the constructor "initializer list" that works. For...
8
by: Alex Buell | last post by:
I've just written the below as an exercise (don't worry about the lack of checking), but I was wondering why I needed to write a struct with an operator() as a parameter to supply to the STL...
5
by: Bob Altman | last post by:
Hi all, Here's a really basic C++ syntax question: How do I initialize an array, whose size is a literal, to all zeros in its initializer without coding a loop to do it? For example: ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.