473,791 Members | 2,947 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<<(Bina ryOStream& 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(c onst 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 2599

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<<(Bina ryOStream& 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(c onst 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
3498
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 possible to change the behavior of operator << so it will stream numeric values as their actual values when an ofstream is in binary mode. I did not, however, find any information on how this can be accomplished. What is involved in getting this...
103
48765
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 says about it. What the heck does the binary flag mean? -- If our hypothesis is about anything and not about some one or more particular things, then our deductions constitute mathematics. Thus mathematics may be defined as the subject in...
28
2807
by: wwj | last post by:
void main() { char* p="Hello"; printf("%s",p); *p='w'; printf("%s",p); }
5
5314
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 quotes), and also some more text like this and file name (e.g. "Content-Disposition: form-data; name="upload_file"; filename="C:\testing\myfile.da<WBR>­t" Content-Type: application/octet-stream")
3
3488
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. -Regards Arun
26
4322
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 Internet Explorer, XPCOM/XPConnect with Mozilla/Firefox). I am writing client-side code that will generate binary data for producing a GIF file through an OBJECT element. (The GIF is an image of a line and points on a two-axis plot.)
2
4641
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 outside the home network) to the Web Server and then retrieve the file back from the web server to the client.
68
5262
by: vim | last post by:
hello everybody Plz tell the differance between binary file and ascii file............... Thanks in advance vim
12
16170
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 result in premature end of input. Instead, you can use the library function feof(), which can be used for both binary- and text-mode files: int feof(FILE *fp);
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10419
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10201
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9987
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7531
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5424
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5552
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4100
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2910
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.