From -3198315301171772003
X-Google-Thread: 7894ca11fe,191c67e7ffa6c8e7
X-Google-Attributes: gid7894ca11fe,public,usenet
X-Google-NewGroupId: yes
X-Google-Language: ENGLISH,ASCII-7-bit
Path: g2news1.google.com!news2.google.com!news1.google.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!news.giganews.com.POSTED!not-for-mail
NNTP-Posting-Date: Wed, 27 Oct 2010 01:30:02 -0500
Return-Path: <cppmods@assassin.cs.rpi.edu>
X-Hash: SCt|4e067be5e2a2bb10f67dc17eaabd661f4998c925|9b4d6473cf396727ec79bcc87eb51039
X-Countries: United States
X-SMTP-From: accepted <cppmods@assassin.cs.rpi.edu> assassin.cs.rpi.edu [128.113.126.17] (assassin.cs.rpi.edu) {United States}
DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=cs.rpi.edu; h=date
	:to:sender:message-id:from:subject:references:content-type; s=
	default; i=128.113.126.17@cs.rpi.edu; t=1288160552; x=
	1288765352; l=3357; bh=zVnsv1MDBYGjITTxzj7wExcZz9Q=; b=UCLQz7tMy
	3EGsEdgtJBNm0/Ecb2YmfFx/TyusE+gaCTbuOl+N3WFFHDaJebXGgQ68zNzkkgr/
	d5mage6YBL28n5fmabmW4cFQe+iAExCogOJQ1HYJiVQhSm0e9qADvQazd5fYsA1a
	HGUdegsXL2231PzpUsGXAmJ4/GP6XOqlX4=
DomainKey-Signature: a=rsa-sha1; c=nofws; d=cs.rpi.edu; h=date:to:sender
	:message-id:from:subject:references:content-type; q=dns; s=
	default; b=V253kBjI+qpNiSYbTIHnqDXXJG/e4csrpEDCn+B12YubryTF0O/wE
	keE25YkG8fCptyEtsHssrxFatWjwN05xYbMzgXgNPTtI7nyZPP1jgo8Pd+9FgsJt
	BnDl3VAbVH43W9QETIWbaMPUjHyNHQtD8Df92SlQKoj8mreXdngPqQ=
X-Spam-Report: Spam Report from cliffclavin.cs.rpi.edu (SA:3.2.5):
	-1.8 ALL_TRUSTED            Passed through trusted hosts only via SMTP
	3.4 HEADER_SPAM            Bulk email fingerprint (header-based) found
	-0.9 BAYES_00               BODY: Bayesian spam probability is 0 to 1% [score: 0.0000]
	0.2 SARE_HEAD_HDR_APPROV   Message headers used which identify spam
	0.0 AWL                    AWL: From: address is in the auto white-list
X-Spam-Info: 0.9, local;
	ALL_TRUSTED,AWL,BAYES_00,HEADER_SPAM,SARE_HEAD_HDR_APPROV
X-Spam-Scanned-By: cliffclavin.cs.rpi.edu using SpamAssassin 3.2.5
X-Virus-Scanned-By: cliffclavin.cs.rpi.edu
To: (Usenet)
Sender: std-c++-request@ruralroute.cs.rpi.edu
Approved: james.dennett@gmail.com 
Message-ID: <fa111668-869a-450a-b5fa-be84f9023e63@n26g2000yqh.googlegroups.com>
Newsgroups: comp.std.c++
From: James Kanze <james.kanze@gmail.com>
Subject: Re: reinterpret_cast<char *>(&some_int) 
Organization: http://groups.google.com
References: <402768.38178.qm@web110410.mail.gq1.yahoo.com>
 <1bb08f2a-854d-4d6c-b686-42a84dd8c2c2@t13g2000yqm.googlegroups.com>
 <842870f3-3d2f-46aa-9b4e-b463ac5b4af0@l14g2000yqb.googlegroups.com>
Content-Type: text/plain; charset=ISO-8859-1
X-Original-Date: Mon, 25 Oct 2010 12:04:27 -0700 (PDT)
X-Submission-Address: std-c++@netlab.cs.rpi.edu
X-Scanned-By: MIMEDefang 2.67 on 128.113.126.25
Date: Wed, 27 Oct 2010 01:22:32 CST
Lines: 85
X-Usenet-Provider: http://www.giganews.com
X-Trace: sv3-XPxH3hnYVVODsAMQMNl9vOhiZ/G/0LfjLUYX6DSRVjyAm06jiHtt4co6hSxIPW2FWoMfK7QzhNmmqJ9!iYh0QSbMUwOL5IFQ4LXCzysMY1IWtCq2bpdUeYmYsAceDTWF+j9fvaPh4bxvg+Q716YtOWMXgdhh!ACBjdwQt3xqPZCopfYaeuJviXHY=
X-Complaints-To: abuse@giganews.com
X-DMCA-Notifications: http://www.giganews.com/info/dmca.html
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40
X-Original-Bytes: 6555
Xref: g2news1.google.com comp.std.c++:3056

On Oct 23, 9:06 am, Trigve <trig...@gmail.com> wrote:
> Thank you for the reply,

> On 21. Okt, 00:14 h., James Kanze <james.ka...@gmail.com>
> wrote:> On Oct 13, 6:52 pm, Trigve Siver <trig...@yahoo.com>
> wrote:

> > >  On 12. Okt, 02:06 h., James Kanze <james.ka...@gmail.com>  wrote:
> > > > It is and it isn't.  About all you can say is that you're  more
> > > > or less guaranted to pass the address of the int into
> > > >  input.read.  Since this function expects the address of a char
> > > > buffer,  and will write it as such, just about anything can
> > > > happen after  that.
> > > Is there ANY portable way to read binary data of PODs from file?

> > Unless you know the format of the data in the file, there's no
> > way to read it, portable or not.  If you know the format, you
> > read the bytes, and assemble them as the format requires.

> Ok, the last example :):
>
> std::fstream file("temp.bin", std::ios_base::in | std::ios_base::out |
> std::ios_base::binary | std::ios_base::trunc);
> char buffer[4];
> std::int32_t value_write = 1234;
> std::int32_t value_read = 0;
> std::memcpy(buffer, &value_write, sizeof(value_write));
> file.write(buffer, 4);
> file.flush();
> std::memset(buffer, 0, 4);
> file.seekg(0, std::ios_base::beg);
> file.read(buffer, 4);
> std::memcpy(&value_read, buffer, sizeof(value_read));
> assert(value_write == value_read);

> Is the code above portable in following circumstances:

> 1 - using the code on the same platform
> 2 - using the code across multiple platform with same endianess
> 3 - using the code across multiple platform

The code, as written, is perfectly portable, since you're
reading the data from the same process (the same compiled image)
that wrote it.  Beyond that, you should be able to read the data
from a different process if the two processes were compiled with
the "same" compiler: same version, same compilation flats,
running on the same hardware, under the same OS (although the
standard doesn't even make this guarantee).  Beyond that, all
bets are off.

> > > Only method I can think of is using "char *" buffer and  then
> > > "std::memcpy()" to target of POD type (3.9/2).

> > Which will be about the same as using reinterpret_cast.  You
> > will get just about anything.

> Do you mean that the following code is as much portable as the code
> above?:

> std::fstream file("temp.bin", std::ios_base::in | std::ios_base::out |
> std::ios_base::binary | std::ios_base::trunc);
> std::int32_t value_write = 1234;
> std::int32_t value_read = 0;
> file.write(reinterpret_cast<char *>(&value_write),
> sizeof(value_write));
> file.flush();
> file.seekg(0, std::ios_base::beg);
> file.read(reinterpret_cast<char *>(&value_read), sizeof(&value_read));
> assert(value_write == value_read);

Yep.  The two versions are, in practice, pretty much equivalent.

In practice, such hacks are fine for temporarily spilling data
to disk, to be reread later by the same process.  For anything
else, however, you need to define a format, and write it.

--
James Kanze

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]



