
On Fri, Apr 29, 2011 at 7:13 AM, Max Sobolev <macsmr@ya.ru> wrote:
On 29.04.2011 11:52, Mathias Gaunard wrote:
int main() { fstream f("binary_stream_example.dat", std::ios_base::trunc | std::ios_base::in | std::ios_base::out | std::ios_base::binary);
int32_t x = 0x01020304; int32_t y = 0;
f<< bin(x); // write 4 bytes f.seekg(0); f>> bin(y); // read 4 bytes
I think it's a bad idea.
If you want to do that kind of thing, you should use the streambuf abstraction directly, along with its sgetn and sputn member functions. It will be much more efficient.
I am not agree. (If you *want* to use the streambuf directly, you should :) But don't force everyone to use this low-level tool.)
I think binary stream IO shouldn't be implemented on the manipulator-like basis, an independent raw_stream class with minimal and full functionality is better. Then below I don't tied to proposed syntax (on the manipulator-like basis).
A stream provide automatic "type deduction" / type safety, therefore an user shouldn't specify the type by hand:
double d; char c; raw_stream raw; raw << d << c; raw >> d >> c;
That's a different problem. It addresses the case where all I/O is raw binary, yet still stream oriented. The use case I'm concerned with is where a regular stream sometimes needs to treat integers as binary. --Beman