15.5. Unformatted I/O using read, write and gcount
Unformatted input/output is performed using the read and write member functions of istream and ostream, respectively. Member function read inputs some number of bytes to a character array in memory; member function write outputs bytes from a character array. These bytes are not formatted in any way. They are input or output as raw bytes. For example, the call
char buffer = "HAPPY BIRTHDAY"; cout.write( buffer, 10 );
cout.write( "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 10 );
displays the first 10 characters of the alphabet.
The read member function inputs a designated number of characters into a character array. If fewer than the designated number of characters are read, failbit is set. Section 15.8 shows how to determine whether failbit has been set. Member function gcount reports the number of characters read by the last input operation.
Figure 15.7 demonstrates istream member functions read and gcount and ostream member function write. The program inputs 20 characters (from a longer input sequence) into character array buffer with read (line 15), determines the number of characters input with gcount (line 19) and outputs the characters in buffer with write (line 19).
Figure 15.7. Unformatted I/O using the read, gcount and write member functions.