473,382 Members | 1,651 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

binary I/O

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
5 2921
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: yaipa | last post by:
What would be the common sense way of finding a binary pattern in a ..bin file, say some 200 bytes, and replacing it with an updated pattern of the same length at the same offset? Also, the...
20
by: Christian Stigen Larsen | last post by:
A signed int reserves one bit to signify whether a number is positive or negative. In light of this, a colleague asked me whether there existed an int in C++ that was -0, a zero with the negative...
3
by: Tron Thomas | last post by:
What does binary mode for an ofstream object do anyway? Despite which mode the stream uses, operator << writes numeric value as their ASCII representation. I read on the Internet that it is...
103
by: Steven T. Hatton | last post by:
§27.4.2.1.4 Type ios_base::openmode Says this about the std::ios::binary openmode flag: *binary*: perform input and output in binary mode (as opposed to text mode) And that is basically _all_ it...
9
by: Ching-Lung | last post by:
Hi all, I try to create a tool to check the delta (diff) of 2 binaries and create the delta binary. I use binary formatter (serialization) to create the delta binary. It works fine but the...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
68
by: vim | last post by:
hello everybody Plz tell the differance between binary file and ascii file............... Thanks in advance vim
5
by: bwv539 | last post by:
I have to output data into a binary file, that will contain data coming from a four channel measurement instrument. Since those data have to be read from another C program somewhere else, the...
10
by: rory | last post by:
I can't seem to append a string to the end of a binary file. I'm using the following code: fstream outFile("test.exe", ios::in | ios::out | ios::binary | ios::ate | ios::app)...
16
by: Erwin Moller | last post by:
Why is a binary file executable? Is any binary file executable? Is only binary file executable? Are all executable files binary? What is the connection between the attribute of binary and that of...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.