Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #7858
| 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 | 2011-07-16 13:18 +1200 |
| 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> |
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
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