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

fstream, overloading >> and << for serialization of binary classes

Hello,

Is it considered sane/good practice to write a global operator for the
insertion and extraction operators of an fstream in binary mode to
serialize a binary class, or are they strictly meant for formatted text
input and output?

Let's imagine, for example, that I had a standard Windows BMP file (I am
aware that C++ has no concept of a BMP - this is simply putting my
question in a simple context). If I wanted to load it into a BMP class,
would it be horrible and wrong to write an operator to enable an
ifstream in binary mode to do this?

BMP myBitmap;
ifstream fileIn("filename.bmp", ios::binary);
fileIn >myBitmap;

I'm guessing that it would, as the functionality would depend on the
flags of the stream, and that's hideous and specific.

However, there doesn't seem to be an obvious way of
serializing/de-serializing to specific binary formats in a nice way in
standard C++. Perhaps there is something in Boost...?

(I am aware that I could just do fileIn.read, but I am working with a
streaming format and would rather my access methods would fit in with a
pre-existing framework).

Thanks.

- Wayne
Dec 23 '06 #1
2 2487
"Wayne Marsh" <do********@invalid.orgwrote in message
news:45***********************@news.zen.co.uk...
Hello,

Is it considered sane/good practice to write a global operator for the
insertion and extraction operators of an fstream in binary mode to
serialize a binary class, or are they strictly meant for formatted text
input and output?

Let's imagine, for example, that I had a standard Windows BMP file (I am
aware that C++ has no concept of a BMP - this is simply putting my
question in a simple context). If I wanted to load it into a BMP class,
would it be horrible and wrong to write an operator to enable an ifstream
in binary mode to do this?

BMP myBitmap;
ifstream fileIn("filename.bmp", ios::binary);
fileIn >myBitmap;

I'm guessing that it would, as the functionality would depend on the flags
of the stream, and that's hideous and specific.

However, there doesn't seem to be an obvious way of
serializing/de-serializing to specific binary formats in a nice way in
standard C++. Perhaps there is something in Boost...?

(I am aware that I could just do fileIn.read, but I am working with a
streaming format and would rather my access methods would fit in with a
pre-existing framework).

Thanks.

- Wayne
Personally, I think it would be a useful thing to write a operator>for a
BMP or other classes, and yes, it would depend on the flags of the ifstream,
but don't overrides already?
Dec 23 '06 #2
Hello Wayne!
Wayne Marsh wrote:
Is it considered sane/good practice to write a global operator for the
insertion and extraction operators of an fstream in binary mode to
serialize a binary class, or are they strictly meant for formatted text
input and output?
The latter! IOStreams are intended for formatted textual I/O only. The
"binary" flag has a relatively minor effect (and none on some
platforms).
If you want to use binary (formatted; it is still formatted in some
form
although not human readable but adhering to some conventions to
transfer binary data) formatted stream, create classes which internally
depend on the actual I/O layer of IOStreams, i.e. their stream buffers.
James Kanze and Dietmar Kuehl have discussed a system like this in
the past.

Good luck, Denise

Dec 23 '06 #3

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

Similar topics

13
by: dpj5754 | last post by:
Is there a simple and determinist way to make the difference between the 2 sequences: <XX></XX> and <XX/> The EndElement callback does not provide this information.
8
by: Daniel Hansen | last post by:
I know this must seem totally basic and stupid, but I cannot find any reference that describes how to control the spacing between <p>...</p> and <div>...</div> blocks. When I implement these on a...
6
by: Armando | last post by:
Hallo ! I habe some error in my programm,because i use <fstream.h>,I want to use <fstream> but i donīt know which fonctions i must modify in my program ? Thanks you for your help. Armando.
6
by: Rate Spring | last post by:
hi, in win2000 and VC6, I operate a txt file. When I write a line data into file and add "\r\n" at the end of line, I find: fstream << "\r\n" write 3 chars : 0D 0D 0A fstream << "\n" ...
1
by: Macca | last post by:
Hi, I have been using <fstream.h> in stdafx.h,(i'm using MFC) to output to text files. I have now started to use vectors and when i added #include <vector> using namespace std; to...
2
by: Fredrik Melin | last post by:
Hi, I have a vendor that requires me to send empty value, e.g. <OrderIDInfo /> problem is that my xslt need to add attributes, doing this <OrderIDInfo> <xsl:attribute name="orderID">...
4
by: nils | last post by:
Hi all, Recently I started migrating a gcc project to Visual Studio C++ (dotnet). The problem is that I cannot include any iostream header-file: #include <fstream> int main (int argc, char *...
4
by: adamrobillard | last post by:
Hi, I have always used fopen and FILE* to load and save structures to file. I am trying to convert all the older code to use proper C++ calls... the following code works properly but I would...
4
by: jelle | last post by:
Hi, I use python quite a bit to couple different programs together. Doing so has been a _lot_ easier since subprocess came around, but would really like to be able to use the succinct shell...
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
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...

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.