Skip to main content
gamedev.net
🔒 Locked

Stringstream for binary data

Started by antareus May 10, 2003 at 10:54 PM 4 replies 3.7k views
Original Post
antareus
antareus
I read the chapter in Stroustrup's C++ book on streams in an attempt to figure this out, but just got more confused. I want to be able to use operators >> and << for reading and writing numbers as binary data to a stream. All the streams support right now is textual data. I'd like to make it fit into the standard stream hierarchy. What do I change in order to do this? Make a new locale? Or make a new type of stream? Edit: forgot to mention that I'd like to avoid the client of this class using read()/write() member functions as well. [edited by - antareus on May 10, 2003 11:55:22 PM]
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
Oluseyi
Oluseyi
The new IOstream classes are templates, so overloading the insertion and extraction operators for your desired type at an appropriately low level should yield the desired results. For a more complete solution, simply derive a new class from std::streambuf (IIRC; Fruny told me he did something like this to create an OpenGLstream class).
antareus
antareus
quote:
Original post by Oluseyi
The new IOstream classes are templates, so overloading the insertion and extraction operators for your desired type at an appropriately low level should yield the desired results. For a more complete solution, simply derive a new class from std::streambuf (IIRC; Fruny told me he did something like this to create an OpenGLstream class).

I should have clarified more.

I want to be able to extract and insert basic types into a stream based on their binary representations. Operator << and >> are for textual extraction only, if you open a file stream in binary mode, you cannot use them, you have to use read() and write() member functions to read them in.

An example:
I want to write the number 4 into a stream. If I used a normal stringstream it''d yield the ASCII value for ''4''.

What I''m wanting to do is write not the ASCII value, but the integer itself to the stream like this: 00 04 (high bits first here). I''d provide a manipulator that allows you to reverse this (network byte order).
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
Kylotan
Kylotan
quote:
Original post by antareus
An example:
I want to write the number 4 into a stream. If I used a normal stringstream it''d yield the ASCII value for ''4''.

What I''m wanting to do is write not the ASCII value, but the integer itself to the stream like this: 00 04 (high bits first here). I''d provide a manipulator that allows you to reverse this (network byte order).

But "00 04" is not binary. You''re probably talking about hexadecimal.

[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]
Oluseyi
Oluseyi
You can convert the typed data to binary yourself. Otherwise you must use read and write.
antareus
antareus
quote:
Original post by Oluseyi
You can convert the typed data to binary yourself. Otherwise you must use read and write.

Yes. I have written a stream class that acts like it, I''m asking how to fit it into the iostream hierarchy so I can use << and >> with the basic types mostly.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.