By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,156 Members | 989 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,156 IT Pros & Developers. It's quick & easy.

binary I/O

P: n/a
Hello

Is there something more elegant in C++ to read/write data
binary into a stream than for example:

out.write(reinterpret_cast<const char *>(&long_var), sizeof(long_var));
in.read(reinterpret_cast<char *>(&unsigned_var), sizeof(unsigned_var));

The need for reinterpret_cast<> looks suspicious.

Thanks
Volker

Jul 19 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
WW
vw at iep dot tu-graz dot ac dot at" <"vw at iep dot tu-graz dot ac dot
at wrote:
Hello

Is there something more elegant in C++ to read/write data
binary into a stream than for example:

out.write(reinterpret_cast<const char *>(&long_var),
sizeof(long_var)); in.read(reinterpret_cast<char *>(&unsigned_var),
sizeof(unsigned_var));

The need for reinterpret_cast<> looks suspicious.


Yes. Use static_cast. *Never* use reinterpret_cast, unless you are doing
something deliberately non-portable.

--
WW aka Attila
Jul 19 '05 #2

P: n/a
WW wrote:
vw at iep dot tu-graz dot ac dot at" <"vw at iep dot tu-graz dot ac dot
at wrote:
Hello

Is there something more elegant in C++ to read/write data
binary into a stream than for example:

out.write(reinterpret_cast<const char *>(&long_var),
sizeof(long_var)); in.read(reinterpret_cast<char *>(&unsigned_var),
sizeof(unsigned_var));

The need for reinterpret_cast<> looks suspicious.

Yes. Use static_cast. *Never* use reinterpret_cast, unless you are doing
something deliberately non-portable.


You can't static_cast from long* to const char* directly, though. You'd
have to use double static_casts, right?

static_cast<const char *>(static_cast<void *>(&long_var))

Or I suppose you could define a new type of cast:

template<class D, class S>
D safe_reinterpret(const S &source)
{
return static_cast<D>(static_cast<void *>(source));
}

OK, that's rather ugly, and not very safe at all, but it might be a good
solution with some more work.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #3

P: n/a
Kevin Goodsell wrote:
....

Or I suppose you could define a new type of cast:

template<class D, class S>
D safe_reinterpret(const S &source)
D terribly_unsafe_reinterpret(const S &source)

bad ... bad bad :=)
{
return static_cast<D>(static_cast<void *>(source));
}

OK, that's rather ugly, and not very safe at all, but it might be a good
solution with some more work.


What kind of work ?

Portability in this case is certainly an issue, it has all the problems
associated with writing binary files - see the post I wrote 7 days ago.

http://groups.google.com/groups?hl=e...concentric.net
G

Jul 19 '05 #4

P: n/a
Gianni Mariani wrote:
Kevin Goodsell wrote:
...

Or I suppose you could define a new type of cast:

template<class D, class S>
D safe_reinterpret(const S &source)

D terribly_unsafe_reinterpret(const S &source)

bad ... bad bad :=)
{
return static_cast<D>(static_cast<void *>(source));
}

OK, that's rather ugly, and not very safe at all, but it might be a
good solution with some more work.

What kind of work ?


Well... if you could make sure the destination type is void*, char*,
unsigned char*, or signed char* (or a const and/or volatile qualified
variant of one of those), and the source type is a pointer, then it
should be safe, I think. Don't know how you'd do that, though.

But even as it is I don't think it's less safe than a reinterpret_cast
(other than the fact that it misrepresents itself as being safe).

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #5

P: n/a
Kevin Goodsell wrote:
Gianni Mariani wrote:
Kevin Goodsell wrote:
...

Or I suppose you could define a new type of cast:

template<class D, class S>
D safe_reinterpret(const S &source)


D terribly_unsafe_reinterpret(const S &source)

bad ... bad bad :=)
{
return static_cast<D>(static_cast<void *>(source));
}

OK, that's rather ugly, and not very safe at all, but it might be a
good solution with some more work.


What kind of work ?

Well... if you could make sure the destination type is void*, char*,
unsigned char*, or signed char* (or a const and/or volatile qualified
variant of one of those), and the source type is a pointer, then it
should be safe, I think. Don't know how you'd do that, though.


OK - that's a good idea. I should probably do that for the network
order template.

Jul 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.