tags:

views:

548

answers:

2

I've always read and been told that when dealing with binary files that one should use read() and write() as opposed to the << and >> operators as they are meant for use with formatted data. I've also read that it is possible to use them, but it is an advanced topic, which I can't find where anyone dives into and discusses.

I recently saw some code which did the following:

 std::ifstream file1("x", ios_base::in | ios_base::binary);
 std::ofstream file2("y", ios_base::app | ios_base::binary);

 f1 << f2.rdbuf();

When I pointed out the use of the << operator with the binary file, I was told that the rdbuf() call returns a streambuf * and that << overloads the streambuf* and does a direct copy with no formatting and is thus safe.

Is this true and also safe? How about efficiency? Any gotchas? Details would be much appreciated.

Thanks!

+2  A: 

Yes (see 27.6.2.5.3/6 where the overload of << for streambuf is described).

AProgrammer
Is that a page in the standard or what? :) A link would be nice!
Skurmedel
This is a reference to a paragraph in the standard. The standard itself is not publicly available. Some drafts are but I don't have a link handily.
AProgrammer
Okay, thanks for clarifying. They should make the standard public.
Skurmedel
Selling it is how they make their money, so unlikely. Would be nice though I agree. Late draft for the 98 version is available at ftp://ftp.research.att.com/pub/c++std/WP/CD2/
KTC
Who is "they"? ISO and national bodies are not profit organization. The money they get from selling standards pay their operation cost and popular standards in fact support less popular one. People on the committee and their employer already support the process, by giving their time, paying for their travel and lodging for the meetings, sponsoring the meeting so that rooms and so on are available and often by paying a membership fee. The arrangement is not really designed for PL, but PL are for these organizations a pretty peculiar domain and making exceptions doesn't make sense for them.
AProgrammer
Thank you KTC.
Skurmedel
+2  A: 

It's entirely safe and a reasonable way to copy streams.

Note that it also allows stuff like:

std::ifstream file_in1("x1", ios_base::in | ios_base::binary);
std::ifstream file_in2("x2", ios_base::in | ios_base::binary);
std::ofstream file_out("y", ios_base::app | ios_base::binary);

file_out << file_in1.rdbuf() << "\nand\n" << file_in2.rdbuf();
MSalters
That's pretty neat.
Skurmedel
Do you have details as to why this is safe? I know you can do this and it seems to work, but my curiosity lies in the fact that it is a common thing to see where references state that << and >> are not for binary data. For instance does this work for text files that are opened as binary as well as for images just the same?
RC
<< and >> are overloaded operators. That is to say, there are many implementations. In general, these are intended for text and will format their second operand. However, the streambuf overload is fundamentally different from all the others.
MSalters