CIS 29 - Notes for Thursday, 1/14

Announcements and Reminders

  • Attendance using Zoom waiting room
  • Project Group assignments will be announced on Tuesday
  • Mentors@DeAnza program: signups close on January 15th


Binary File I/O

Remember Remember to open a binary file in binary mode      

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.

int peek();


Sets the next read position in the input stream.  Note, as of C++ 11, seekg will clear the EOF bit, if set.

stream& seekg (streampos  pos);
istream& seekg (streamoff  offset, ios_base::seekdir way);

ios_base::seekdir can be one of three constants

Constant   Meaning
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.

streampos tellg();

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.

streampos tellp();

Example 3-2 – ostream member functions

Example 3-3 – binary file I/O: a practical example