473,624 Members | 2,122 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.stat us),buf,1);
memcpy(&(d.time stamp),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 2740

"Kai Wu" <ka********@fas tmail.cn> wrote in message
news:Sq******** ***********@new s1.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.stat us),buf,1);
memcpy(&(d.time stamp),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::binar y); // 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.times tamp,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********@fas tmail.cn> wrote in message
news:Sq******** ***********@new s1.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.stat us),buf,1);
memcpy(&(d.time stamp),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::binar y); // 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.times tamp,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********@fas tmail.cn> wrote in message
news:l8******** ***********@new s2.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::binar y); // 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.times tamp,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********@fas tmail.cn> wrote in message news:Sq******** ***********@new s1.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.stat us),buf,1);
memcpy(&(d.time stamp),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************* **********@hotm ail.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********@fas tmail.cn> wrote in message
news:l8******** ***********@new s2.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::binar y); // 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.times tamp,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
2007
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 the proper way to do this, eg, without the pointers and such). As is probably obvious, I am not terribly knowledgable here. The *fs.of<< ... statement in main() does nothing in gcc 3.2.2 and gives a seg fault in gcc 3.2 (Mandrake linux 9.1 and...
18
4569
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
4434
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 contents) my problem now is how can i grab all the file contents in one swoop and put it in a string for editing? I hate to go through the file and copy line by line:
10
3015
by: wenmang | last post by:
hi, I have following: struct array1 { int id; char *name; }; struct array2 {
18
2857
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
1721
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
2149
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 example, suppose MyStruct has 3 int members. I've tried something like this: class Test {
8
2028
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 transform() function? #include <algorithm> #include <iostream> #include <fstream> #include <iterator> #include <vector> #include <string>
5
1627
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: #define N 5 bool myData; // Don't want to do this:
0
8236
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8173
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8621
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8475
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7159
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6110
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4079
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4174
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1482
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.