Announcements and Reminders
Comments on last night's Arrays and Pointers Demo
Binary File I/O
What is a binary file?
istream member functions
Read a specified number of characters from an input stream and stores them in a char array. The array is not null-terminated.
istream& read (char* s, streamsize n);
Returns the next character to be read without extracting it from the input stream.
Sets the next read position in the input stream. Note, as of C++ 11, seekg will clear the EOF bit, if set.
istream& seekg (streampos pos);
istream& seekg (streamoff offset, ios_base::seekdir way);
ios_base::seekdir can be one of three constants
beg Beginning of the input stream
cur Current position in the input stream
end End of the input stream
Returns the next read position in the input stream.
Example 3-1 – istream member functions
What's wrong with this example and how do you determine file size?
ostream member functions
Write a specified number of characters to an output stream
ostream& write (const char* s, streamsize n);
Sets the next write position in the output stream. Note, as of C++11, seekp will work, even if the EOF bit is set, but it will not change the value of the EOF bit.
ostream& seekp (streampos pos);
ostream& seekp (streamoff off, ios_base::seekdir way);
Returns the next write position in the output stream.
Example 3-2 – ostream member functions
Example 3-3 – binary file I/O: a practical example
What is the difference between writing out a C-string and a string in binary?