TITLE: binary I/O using and streams (Newsgroups: comp.std.c++, 6 Feb 98) CLAMAGE: >> It sounds like you are trying to output binary values with the << >> operator. By definition, the << operator converts its operand to a >> text representation, independent of the mode of the destination >> stream. >> >> Use the "write" function to output binary data to a stream that >> was opened in binary mode. >> >> (It's the same as in C. You don't use printf to output binary data, >> you use fwrite.) MARSHALL: "David Marshall" >But what if you want to write binary data using the << and >> operators? CLAMAGE: clamage@Eng.sun.com (Steve Clamage) Perhaps it would help to review the design strategy of iostreams. Iostreams have two layers. At the bottom is the transport layer. It provides the interface between the source or sink and the top layer. In particular, a filebuf knows about different file types -- at minimum "text files" and "binary files" -- and how to convert data between the file system representation and the internal program representation. The top iostream layer is the formatting layer. It provides the interface between the user code and the transport layer. It doesn't know or care about transport details, or whether the source or sink is a text file, binary file, string, socket, or a programmable machine tool. By design, the shift operators perform formatting such as text-binary conversion, inserting pad characters, and so on, sending the converted and formatted data to the transport layer. By design, the read and write functions pass through their data unchanged to the transport layer. This design was not intended to support unformatted I/O using shift operators. In particular, the shift operators were not designed to be overridden or replaced. It may be possible to get the effect you want, but it is probably difficult and may be unreliable. In addition, it may be confusing within your program if "target << int_value" sometimes does not involve a text conversion. I agree it is less pleasant to have to say target.write(&val1, sizeof(val1)); target.write(&val2, sizeof(val2)); instead of target << val1 << val2; You could create manipulators that take values, call the write function, and return the stream. Then you could write target << send(val1) << send(val2);