(Latest Revision: 05/05/2000)
Week 12 Notes for CS 1500 -- Spring 2000
(this is draft material)
* Take roll
* Look at the schedule
FILE STREAM INFORMATION
* "ios" is the base class for stream I/O operations.
* "istream" and "ostream" are subclasses of "ios."
+ istream is specifically for input. "cin" is an
instance.
+ ostream is specifically for output. "cout" is an
instance.
* "ifstream" is a subclass of istream, tailored for
FILE input.
* "ofstream" is a subclass of ostream, tailored for
FILE output.
* In order to use ifstream or ofstream objects you
need this directive:
#include <fstream.h>
* These kinds of statements declare and open a
file stream:
ifstream infile("raw.dat") ;
ofstream outfile("process.dat") ;
* Declaration and open can be done separately:
ifstream infile ;
infile.open ("raw.dat") ;
ofstream outfile ;
outfile.open("process.dat") ;
* the value of infile or outfile will be ZERO if the
open operation fails, else non-ZERO. One can use
this fact to test for failure.
* we can use extraction with an ifstream:
infile >> num ;
* we can use insertion with an ofstream:
outfile << num ;
* An expression such as (infile >> num) or
(outfile << num) has a value of ZERO if the
operation fails, else non-ZERO. One can use this
fact to test for failure to do the operation.
* One can read individual characters from an ifstream
like this:
char ch ;
infile >> ch ;
infile.get(ch) ;
The first method skips white space, the second
does not. infile.get() also returns a value of
ZERO or non-ZERO.
* Formatting works the same with the ifstream and
ofstream classes as it does with cin and cout.
* Closing a file:
infile.close() ;
outfile.close() ;
* After closing, a stream can be re-used to open a
different file (or the same one again).
infile.open("raw2.dat")
REFERENCE PARAMETER INFORMATION
* Previously we have used parameters that are passed
by value -- a copy of the parameter goes to the
function -- not the original. The original is
therefore unchanged by anything the function does
to the copy.
* One can also pass a parameter to a function by
reference (but only if the parameter is a variable,
not a constant or an expression). In this mode,
the address of the variable is what the function
actually recieves. When the function examines or
changes the parameter, it goes to the memory
location indicated by the address and reads from or
writes to that location in the memory. That
location is in the memory space accessible to the
caller. Therefore if function X calls function Y
and passes a parameter z to Y by reference, Y can
change the value of z in such a way that the change
is visible to X.
* "We can also use pass by reference to avoid the
time and space involved in copying an argument."
* To pass a ifstream or ofstream by value would mean
copying what might be a large file. This is not
practical.
ARRAY INFORMATION
* Arrays make it easy for a program to work with a large
collection of items that are all of the same data type -- for
example the prices of all the items in a store can be an
array of 1000 floats. Here is how that declaration would
look:
float price[1000] ;
This tells the compiler to reserve a contiguous section of
the memory, enough for 1000 float variables, and to associate
the name "price" with the entire section.
* The array has a single name. The programmer does not have to
declare and name 1000 different float variables.
* The slots in the array are numbered from 0 to some upper
limit. In the example above, the slots are floating point
variables numbered 0 to 999 inclusive. (There are 1000 slots,
but because 0 is the lowest, 999 is the highest. There is no
slot 1000.)
* The programmer can access individual array slots by using
"index notation." For example, the stament price[3] = 5.95
assigns the value 5.95 to the slot numbered 3 (the fourth
slot in the array).
* One of the nicer things about arrays is that the index can be
a variable or expression. For example the statement
for (int i=0; i<1000; i++) price[i] = 0.0 ;
initializes all 1000 of the slots to the value 0.0 through
the use of just one for-loop.
* Also the index to an array can be an input to a program. If
there is a line of input like:
53 6.79
and if the program statement is:
cin >> i >> price[i] ;
then this serves to change price[53] to the value 6.79. If
we want our program to make a long series of price changes,
we can use C++ code like:
cout << "Give an index and float value: " ;
while (cin >> i >> price[i])
{
cout << i << " " << price[i] << endl ;
cout << "Give an index and float value: " ;
}
Then all we have to do is input a series like this to get all
the price changes made that we desire:
42 9.99
193 18.54
53 11.98
17 5.79
438 0.79
* Check out the sample program ex1003.cpp to see how arrays
processing may be done in a program, and how arrays are
passed as parameters to functions.
=========================
MONDAY
=========================
=========================
WEDNESDAY
=========================
=========================
FRIDAY
=========================