Re: Fail to initialize struct from fstream

From: John Harrison (john_andronicus_at_hotmail.com)
Date: 10/09/04


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



Relevant Pages

  • Re: Dos and donts
    ... John used 50k and an LT1124 opamp in his post, ... I would want to run in PSpice so I can use my "world-renowned" loop ... possible VHF oscillations in the transistors themselves, ... came out of chip processing working just ducky. ...
    (sci.electronics.design)
  • Re: For Loop Question
    ... I do understand that I will still occasionally encounter multiple "John ... Smith" occurances. ... I'm reading in a text file to a for loop to process against ... It ends up searching for John not "John Smith" and ...
    (comp.unix.questions)
  • Re: Need help with Loop that is not repeating the function
    ... lat/lon gecodes that I want to feed back into my table. ... because I thought the loop would take care of that. ... "John W. Vinson" wrote: ...
    (microsoft.public.access.formscoding)
  • Re: Can PLL Freq Error be zero?
    ... >> Well, I did specify that Somethingorother was the divider in the loop, ... >> 26 letters on my keyboard, and it's not my fault that some lunatic ... John ...
    (sci.electronics.design)
  • Re: Sydney-X1 FPGA Computer Challenges Commodore, Amiga and Apple
    ... John S. Dyson wrote in message ... the simple empty loop takes about .75 nanoseconds per iteration. ... > for NORMAL instruction streams, where the data/program is cache resident, ... > that 1 instruction per clock cycle is plausible. ...
    (sci.electronics.design)