Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #7858

Re: How to fread ((void *)header, sizeof (struct Sheader), 1, file); in C++

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.musoftware.de!wum.musoftware.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From Ian Collins <ian-news@hotmail.com>
Newsgroups comp.lang.c++
Subject Re: How to fread ((void *)header, sizeof (struct Sheader), 1, file); in C++
Date Sat, 16 Jul 2011 13:18:16 +1200
Lines 70
Message-ID <98c78mFv3qU6@mid.individual.net> (permalink)
References <b6cebe9c-fb96-43b7-836e-95b85ca6f03b@q17g2000vby.googlegroups.com> <98c0ujFv3qU3@mid.individual.net> <b7fb2338-f458-4db4-b556-d91115bd15ac@r9g2000yql.googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Trace individual.net VE5uhRUgg4qohmzsk0Jp7gUgMHTzDyLTlq2t/SNNg6pmtKKema
Cancel-Lock sha1:bIDE/84AmPldQMobMh4FMpotTXU=
User-Agent Mozilla/5.0 (X11; U; SunOS i86pc; en-US; rv:1.9.2.9) Gecko/20101021 Lightning/1.0b2 Thunderbird/3.1.4
In-Reply-To <b7fb2338-f458-4db4-b556-d91115bd15ac@r9g2000yql.googlegroups.com>
Xref x330-a1.tempe.blueboxinc.net comp.lang.c++:7858

Show key headers only | View raw


On 07/16/11 12:23 PM, Hetware wrote:
> On Jul 15, 7:30 pm, Ian Collins<ian-n...@hotmail.com>  wrote:
>> On 07/16/11 10:36 AM, Hetware wrote:
>
>>> I have some ancient c code that reads files stored in raw binary
>>> format.  The first 216 bytes are to be read into an instance of:
>>
>>> struct Sheader
>>> {
>>>     long    attr_start;
>>>     long    form_start;
>>>     short   fsize;
>>>     short   attr_size;
>>>     short   lastfieldidx;
>>>     short   num_inputs;
>>>     char    oldheader[200];
>>> };
>>
>>> The way the C code does this is by fread ((void *)header, sizeof
>>> (struct Sheader), 1, file);  Is there a better way to do this in C++?
>>
>> Not really (but you don't need 'struct" in there), it's awful in C and
>> it will still be awful in C++!
>
> I'm not sure why struct is "awful".

Storing raw binary is awful!

> At this point, its not a question
> of whether I like it or not.  I just need a way to get the first bytes
> of the file read in as POD.  I actually did make Sheader into a class,
> but that really makes little difference.  The following doesn't appear
> to get the correct data into the class members:
>
> #include<iostream>
>
> class Header {
> public:
>    Header(){}
>    Header(std::istream&  in);
>    std::ostream&  print(std::ostream&  out);
> private:
>    long	attr_start;
>    long	form_start;
>    short	fsize;
>    short	attr_size;
>    short	lastfieldidx;
>    short num_inputs;
>    char	oldheader[200];
>
> };
>
>
> Header::Header(std::istream&  in) {
>    in>>attr_start
>      >>form_start
>      >>fsize
>      >>attr_size
>      >>lastfieldidx
>      >>num_inputs;
> }

You are using formatted input operators to read raw data (which is one 
reason raw data is evil).

If you have to read in the raw binary data, you have to use in.read(), 
which will look pretty much the same as the C code.

-- 
Ian Collins

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

How to fread ((void *)header, sizeof (struct Sheader), 1, file); in C++ Hetware <fjolsvit@gmail.com> - 2011-07-15 15:36 -0700
  Re: How to fread ((void *)header, sizeof (struct Sheader), 1, file); in C++ Ian Collins <ian-news@hotmail.com> - 2011-07-16 11:30 +1200
    Re: How to fread ((void *)header, sizeof (struct Sheader), 1, file); in C++ Hetware <fjolsvit@gmail.com> - 2011-07-15 17:23 -0700
      Re: How to fread ((void *)header, sizeof (struct Sheader), 1, file); in C++ Hetware <fjolsvit@gmail.com> - 2011-07-15 17:58 -0700
      Re: How to fread ((void *)header, sizeof (struct Sheader), 1, file);   in C++ Ian Collins <ian-news@hotmail.com> - 2011-07-16 13:18 +1200
        Re: How to fread ((void *)header, sizeof (struct Sheader), 1, file); in C++ Hetware <fjolsvit@gmail.com> - 2011-07-15 18:37 -0700
        Re: How to fread ((void *)header, sizeof (struct Sheader), 1, file); in C++ gwowen <gwowen@gmail.com> - 2011-08-10 03:00 -0700
      Re: How to fread ((void *)header, sizeof (struct Sheader), 1, file); in C++ Arvind <bharti.arvind@gmail.com> - 2011-07-15 20:39 -0700
      Re: How to fread ((void *)header, sizeof (struct Sheader), 1, file); in C++ Nobody <nobody@nowhere.com> - 2011-07-16 13:49 +0100
      Re: How to fread ((void *)header, sizeof (struct Sheader), 1, file); in C++ Todd Carnes <toddcarnes@gmail.com> - 2011-07-16 13:17 +0000
      Re: How to fread ((void *)header, sizeof (struct Sheader), 1, file); in C++ Paul N <gw7rib@aol.com> - 2011-07-16 09:51 -0700
        Re: How to fread ((void *)header, sizeof (struct Sheader), 1, file); in C++ Paavo Helde <myfirstname@osa.pri.ee> - 2011-07-16 12:16 -0500
  Re: How to fread ((void *)header, sizeof (struct Sheader), 1, file); in C++ Paavo Helde <myfirstname@osa.pri.ee> - 2011-07-16 03:46 -0500
    Re: How to fread ((void *)header, sizeof (struct Sheader), 1, file); in C++ Juha Nieminen <nospam@thanks.invalid> - 2011-07-17 13:46 +0000
      Re: How to fread ((void *)header, sizeof (struct Sheader), 1, file); in C++ Robert Wessel <robertwessel2@yahoo.com> - 2011-07-17 12:51 -0500
  Re: How to fread ((void *)header, sizeof (struct Sheader), 1, file); in C++ Krice <paulkp@mbnet.fi> - 2011-07-16 02:44 -0700
  Re: How to fread ((void *)header, sizeof (struct Sheader), 1, file); in C++ hanukas <jukka@liimatta.org> - 2011-07-17 11:32 -0700
    Re: How to fread ((void *)header, sizeof (struct Sheader), 1, file); in C++ Hetware <fjolsvit@gmail.com> - 2011-08-01 09:18 -0700
      Re: How to fread ((void *)header, sizeof (struct Sheader), 1, file); in C++ Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-08-01 20:17 +0000
    Re: How to fread ((void *)header, sizeof (struct Sheader), 1, file); in C++ James Kanze <james.kanze@gmail.com> - 2011-08-07 11:33 -0700

csiph-web