CIS 29 - Notes for Thursday, 1/17

Announcements and Reminders

  • Check attendance sheet
  • First group meeting to be completed by next Friday (start forming groups)
  • Saturday is the last day to add the class
  • Sunday is the last day to drop the class for a full refund and no 'W' grade

Comments on last night's Arrays and Pointers Demo


Binary File I/O

What is a binary file?

istream member functions

read

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);

peek

Returns the next character to be read without extracting it from the input stream.

int peek();

seekg

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

Constant   Meaning
beg        Beginning of the input stream
cur        Current position in the input stream
end        End of the input stream

tellg

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

Write a specified number of characters to an output stream

ostream& write (const char* s, streamsize n);

seekp

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);

tellp

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

What is the difference between writing out a C-string and a string in binary?