How's that? can you show an example?
I can't, but my teachers like to say that if you don't do something right the first time, you end up doing it again and again to fix the problems. It seems to me that using an array is bad because you lock yourself into a buffer size, and using allocated memory is bad because you have to work harder to make it work right and keep it working. The C++ string fixes both of those because it grows on its own and you don't have to manage it.
I don't think std::string class will work with binary data like that. I think the += operator will stop when it encounters the first 0 byte in the iobuf.
Yeah, I tried it. But assign and append let you pick a count for the incoming string. This works.
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
#define SETUP 1
int main() {
string contents;
#ifdef SETUP
ofstream binaryOut( "binary.dat", ios::binary );
contents.assign( "C code.\0C code run.\0Run code run.", 33 );
binaryOut.write( contents.data(), contents.size() );
binaryOut.close();
#else
ifstream binaryIn( "binary.dat", ios::binary );
// Get the binary data
char iobuf[1024];
do {
binaryIn.read( iobuf, sizeof( iobuf ) );
contents.append( iobuf, binaryIn.gcount() );
} while ( binaryIn );
binaryIn.close();
// Print the characters
for ( int i = 0; i < contents.size(); ++i ) {
if ( !isprint( contents[i] ) ) {
cout<< int( contents[i] );
} else {
cout<< contents[i];
}
}
cout<<"\n";
#endif
return 0;
}
and (2) except that old farts like me just perfer using char arrays when the problem suits it better
But does the problem really suit char arrays better or are the old farts just doing what they're comfortable with?

If the file is always a fixed size that's great, but if it's not you have to make a super huge char array to accommodate the expected sizes and add extra logic in case the size is bigger than the array. If you don't want to wrestle with that, you have to dynamically allocate a char array, and that's just a lot of memory management that you don't need to do. It makes the code longer and harder to keep up.