Advertisement

Why is C++ binary IO so flawed?

Started by August 20, 2000 09:48 PM
6 comments, last by Void 24 years, 4 months ago
I must say I am quite disappointed with C++ binary file IO support in the iostream. Firstly, why are the <<,>> operators not overloaded for binary output? I mean can''t the operators detect the stream is opened to binary and use the sizeof(variable type) in read/write. Maybe I should derive another class from istream that does just that but it just seems funny that there is no such thing in the iostream lib. Also why does the read,write method take in a char * pointer instead of a void * like the C function fwrite?? Is there a particular good reason that they have to make me do an annoying type cast even when reading/writing from a binary file into a simple type like
    
float 	f;		// temp var


filestream.read((const char *)&f, sizeof(f));	// read into stream needs a type cast

    
Well, firstly, binary I/O is usually a bad idea when your classes are full of vtable pointers... as many of them will be. I guess they assumed that pure binary I/O would be uncommon, since it''s not all that safe on anything but the inbuilt types.

If you are saving user-defined types, you have to write your own function for it anyway. So, you could do that ''overloading'' (technically an incorrect use of the term, but I know what you mean) yourself.

As for the typecast, I believe that in C++, you would have to use the cast to cast to a void* type anyway, so you don''t lose anything. I expect the ''char'' specification is to make it explicit that you are reading things in a char at a time, which is usually the smallest unit you can read.
Advertisement
Chalk another one up for "Why C is way simpler and easier"

FILE *in = fopen("yay", "rb");
float f;
fread(&f, sizeof(float), 1, in);

And there you go. No subclasses or overloaded operators, or other insane overly complicated features. 3 lines of code.

Die C++ die.
Volition, Inc.
so??

ifstream file("file.dat", ios::binary);
float f;
file.read(char *)&f, sizeof(f));

That's three lines as well...
so c++ binary I/O may be diffrent a bit... you just need
to get used to it. In any case, It's much simpler then reading
binary files in any other language..
(to quote, "Visual Basic")...

whatever, don't get used to >> and <<... those
are fine for console stuff, not for file reading..
And they also make it impossible to parse data with white spaces.
If I want to read a text file, I either use getline() if
I want to read line by line, or get the whole file and parse it later, for example:

    char buffer[256];char WholeFile[256*1024] = ""; //Will read to a maximum size of 256kb.while(!file.eof()) {file.read(buffer, sizeof(buffer);strcat(WholeFile, buffer);}    


















Edited by - aqutiv on August 21, 2000 12:50:29 PM
AquDev - It is what it is.
I hope you relize he doesn''t speak for all C programmers, flamebait just makes stereotypes so much easier. Anyway, if something is that flawed, you can use the C I/O system for binary file i/o.

-----------------------------

A wise man once said "A person with half a clue is more dangerous than a person with or without one."
-----------------------------A wise man once said "A person with half a clue is more dangerous than a person with or without one."The Micro$haft BSOD T-Shirt
Woo ppl, don;t turn into a flame war..

I was just surprised that there is no binary output class that "overloads" the <> operators like so

    class bfstream{..  void operator <<(const &float)  {    stream.write((const char *)&f, sizeof(float));  }}    


Of course this wouldn''t let me do something like os << f << f;but this is off my head.

..
Advertisement
quote: if I want to read line by line, or get the whole file and parse it later, for example:

char buffer[256];char WholeFile[256*1024] = ""; //Will read to a maximum size of 256kb.while(!file.eof()) {file.read(buffer, sizeof(buffer);strcat(WholeFile, buffer);}




Have you tried this? You might like this way as well.

    #include &ltiostream>#include &ltstrstream>#include &ltfstream>using namespace std;int main(){	ifstream	inFile("main.cpp");	strstream	text;	text << inFile.rdbuf();	cout << text.rdbuf() << endl;	return 0;}    


















Edited by - aqutiv on August 21, 2000 12:50:29 PM



YAP-YFIO,
deadlinegrunt

~deadlinegrunt

If they did another class that defaulted to binary, you''d need a lot of them to cover the hierarchy... ibstream, obstream, ibfstream, obfstream, bfstream etc etc... or, to add in yet another compile-time-leeching and compiler-killing template parameter,so that''s a lot of extra stuff for little benefit. Also, a lot of the functions would become semi-redundant: would the text-based streams still need write()?

Basically, the way it is now provides for lots of flexibility. Obviously it''s a shame when the class goes halfway to making I/O very pretty, and a double shame when the non-pretty-looking half is the half you need

This topic is closed to new replies.

Advertisement