CPSC 427a: Object-Oriented Programming
Notes
by
Michael
J.
Fischer
Presented
by
Ewa
Syta
End of File and I/O Errors
What
eof
means
Detecting and properly handling end of file is one of the most confusing
things in C++.
The I/O stream has status flags associated with it. The eof flag is
turned on when the stream attempts to read beyond the end of the
file.
The eof flag may or may not be on after the last byte of the file has been read and returned to the user.
When eof is turned on
Whether eof is on depends on whether the current input operation can complete without looking at the next byte.
Reading an int
What happens depends on the kind of read request. Consider what happens with cin >> x, where x is an int.
Reading an int (cont.)
Examples
The following examples show the remaining bytes in the file, where ␣ represents any whitespace character such as space or newline.
End of file and error handling
There is a fourth status flag also, bad.
I/O functions set status flags after each I/O operation. bad means there was a read or write error on the file I/O. fail means the data was not appropriate to the field, e.g., trying to read a non-numeric character into a numeric variable. eof means that the end of file has been reached. good means that the above three bits are all off.
The whole state can be read with one call to rdstate().
Status functions
Functions are also provided for testing useful combinations of status bits.
As in C, correct end of file and error checking require paying close attention to detail of exactly when these state bits are turned on.
To continue after a bit has been set, must call clear() to clear it.
How to read all numbers in a file
Here’s a simple example for how to add up all the numbers from an open stream in.
Notes on PS2
Problem
Set
2
Notes
The problem set asks to implement the methods declared in the file
Square.hpp
Talking points:
Functions and Methods
Call by value
Like C, C++ passes explicit parameters by value.
Call by pointer
Like C, pointer values (which I call reference values) are the things that can be stored in pointer variables.
Also like C, references values can be passed as arguments to functions having corresponding pointer parameters.
Call by reference
C++ has a new kind of parameter called a reference parameter.
I/O uses reference parameters
How should one choose the parameter type?
Parameters are used for two main purposes:
Sending data to a function: call by value For sending data to a function, call by value copies the data whereas call by pointer or reference copies only an address.
Sending data to a function: call by reference or pointer
Call by reference or pointer allows the caller’s data to be changed.
Use const to protect the caller’s data from inadvertane change.
Ex: int f( const int& x ) or int g( const int* xp ).
Prefer call by reference to call by pointer for input parameters.
Ex: f( 234 ) works but g( &234 ) does not.
Reason: 234 is not a variable and hence can not be the target of a pointer.
(The reason f( 234 ) does work is a bit subtle and will be explained later.)
Receiving data from a function
An output parameter is expected to be changed by the function.
Both call by reference and call by pointer work.
Call by reference is generally preferred since it avoids the need for the
caller to place an ampersand in front of the output variable.
Declaration: int f( int& x ) or int g( int* xp ).
Call: f( result ) or g( &result ).
The implicit argument
Every call to a class member function has an implicit argument, which is the object written before the dot in the function call.
this
The implicit argument is passed by pointer.
In the call ex.advance(3), the implicit argument is ex, and a pointer to ex is passed to advance().
The implicit argument can be referenced directly from within a member function using the keyword this.
Within the definition of advance(), count and this->count are synonymous.