Binary i/o vs Stream i/o

What is the difference between binary i/o and stream i/o?
Within the context of C++ (and C, for that matter), binary I/O is a use of stream I/O

Binary I/O is the practice if reading and writing object representations, rather than values of objects.

For example, an object of type float whose value is 1000.125 consists of four bytes with values 0x00 0x40 0xC8 0x42 (assuming little-endian CPU architecture). If you input/output those four bytes, you are using binary I/O. If you input/output the string "1000.125", you're using text I/O.

1
2
3
4
5
6
7
8
9
10
#include <string>
#include <iostream>

int main()
{
    float f = 1000.125;

    std::cout << f << '\n'; // value
    std::cout.write(reinterpret_cast<char*>(&f), sizeof f) << '\n'; // bytes
}


in both cases stream I/O is used (cout is an object of type ostream, both ostream::operator<< and ostream::write are defined as if they push one byte at a type into that stream).

(same in case of C stream I/O, where both fprintf and fwrite are defined in terms of fputc)
Last edited on
Topic archived. No new replies allowed.