473,322 Members | 1,526 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,322 software developers and data experts.

Clean binary stream

I have a solution for this, but it feels wrong. If anyone could offer
a better one, I'm all ears. (Or technically, eyes.)

Basically, I have a bunch of classes. For concreteness, one is a
Matrix class, but that's only one example, so please don't get too hung
up on it.

I need to output and input these classes. I'd like a nice, pretty,
human readable output, something like:
1 2 3
4 5 6
(note spaces and new lines).

For speed purposes (and yes, I have measured this, and yes, it does
make a difference), I also want a compressed binary version, which
would look like this:
<rows><columns><num>*
(note: no delimiters, you get all the info from the rows and columns).

I definitely need to read and write the binary version, but for some
classes I don't need to read the human readable version.

So what I have looks like this:

/// Output in textual format
std::ostream& operator<<(std::ostream& os, const Matrix& m);
/// Output in binary format
BinaryOStream& operator<<(BinaryOStream& os, const Matrix& m);

where BinaryOStream is defined like this:
class BinaryOStream
{
public:
/** The underlying ofstream wrapped by this class. */
std::ofstream m_out;

public:
/// Create a BinaryOStream from a given filename.
BinaryOStream(const std::string& filename);
~BinaryOStream(void){}

//etc
}

and its constructor opens m_out in binary mode. It also has a series
of non-member << operators for different data types (some of which are
implemented as a template).

What I don't like about the current solution is that I wind up writing
what feels like twice as much code as I should have to. Especially if
I need to read in the format (and Matrix is a bad example for this), it
seems like for most of my classes, the same input should work for both
(imagine another class where the outputs are the same except that the
text has spaces and newlines - I won't cut and paste the code here, but
you get the idea).

I kind of feel like BinaryOStream should inherit from ostream, and
implement the decorator pattern; that way I'd just write stuff for
ostream, and overload for BinaryOStream if I need to, but I can't quite
figure out how to make that work. (In particular, I would love to be
able to output stuff to a 'binary ostringstream' for testing purposes,
instead of to an actual file.)

Has anyone done this sort of thing before, and if so, found a clean
solution?

Michael

Jan 5 '07 #1
1 2567

Michael napsal:
I have a solution for this, but it feels wrong. If anyone could offer
a better one, I'm all ears. (Or technically, eyes.)

Basically, I have a bunch of classes. For concreteness, one is a
Matrix class, but that's only one example, so please don't get too hung
up on it.

I need to output and input these classes. I'd like a nice, pretty,
human readable output, something like:
1 2 3
4 5 6
(note spaces and new lines).

For speed purposes (and yes, I have measured this, and yes, it does
make a difference), I also want a compressed binary version, which
would look like this:
<rows><columns><num>*
(note: no delimiters, you get all the info from the rows and columns).

I definitely need to read and write the binary version, but for some
classes I don't need to read the human readable version.

So what I have looks like this:

/// Output in textual format
std::ostream& operator<<(std::ostream& os, const Matrix& m);
/// Output in binary format
BinaryOStream& operator<<(BinaryOStream& os, const Matrix& m);

where BinaryOStream is defined like this:
class BinaryOStream
{
public:
/** The underlying ofstream wrapped by this class. */
std::ofstream m_out;

public:
/// Create a BinaryOStream from a given filename.
BinaryOStream(const std::string& filename);
~BinaryOStream(void){}

//etc
}

and its constructor opens m_out in binary mode. It also has a series
of non-member << operators for different data types (some of which are
implemented as a template).

What I don't like about the current solution is that I wind up writing
what feels like twice as much code as I should have to. Especially if
I need to read in the format (and Matrix is a bad example for this), it
seems like for most of my classes, the same input should work for both
(imagine another class where the outputs are the same except that the
text has spaces and newlines - I won't cut and paste the code here, but
you get the idea).

I kind of feel like BinaryOStream should inherit from ostream, and
implement the decorator pattern; that way I'd just write stuff for
ostream, and overload for BinaryOStream if I need to, but I can't quite
figure out how to make that work. (In particular, I would love to be
able to output stuff to a 'binary ostringstream' for testing purposes,
instead of to an actual file.)

Has anyone done this sort of thing before, and if so, found a clean
solution?

Michael
Hi.

Stream operator<< is not meant for binary output. It is designed for
formatted output (no matter of ios::binary - on many systems it has no
special meaning).

If you really need binary format, you should use ostream::read and
ostream::write methods. But it has many issues - you have to care about
sizes of types, because there is no guarantee, how many bytes exactly
occupies int, long ot other standard types on different platforms. You
have to care about endianity too. If you ignore these issues, you will
produce non-portable code and it will not probably work for example on
64-bit systems and 32-bit systems.

Jan 5 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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...
28
by: wwj | last post by:
void main() { char* p="Hello"; printf("%s",p); *p='w'; printf("%s",p); }
5
by: Neo | last post by:
Hello: I am receiving a Binary File in a Request from a application. The stream which comes to me has the boundary (Something like "---------------------------39<WBR>­0C0F3E0099" without the...
3
by: Arun | last post by:
Hi, I have simple question to ask. How to write multiple Binary files to the Browser using Asp.Net and Visual C#.net I have seen examples where single binary file is written to browser. ...
26
by: Patient Guy | last post by:
Has anyone written code that successfully manipulates binary file data using Javascript? It might---and in the case of doing I/O, will---make use of browser- specific functions (ActiveX/COM with...
2
by: gauravkhanna | last post by:
Hi All I need some help for the below problem: Scenario We need to send large binary files (audio file of about 10 MB or so) from the client machine (.Net Windows based application, located...
68
by: vim | last post by:
hello everybody Plz tell the differance between binary file and ascii file............... Thanks in advance vim
12
by: Registered User | last post by:
I've read in a book: <quote> With a binary-mode stream, you can't detect the end-of-file by looking for EOF, because a byte of data from a binary stream could have that value, which would...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.