CS 112 - Week 3 Lecture 2 - 2022-09-08
TODAY WE WILL
* announcements
* more C++ review/overview:
* file input/output
* arrays in C++
* if desired:
* see Savitch Ch. 6 - more on interactive
i/o and file i/o
* see Savitch Ch. 7 - review of arrays
=====
* intro to file input and file output in C++
* C++ has a library fstream
that declares, amongst other things,
some useful subclasses of classes from the
iostream library for the purpose of reading
from and writing to files (using a stream approach)
* includes a class ifstream -- input file stream
and a class ofstream -- output file stream
* for reading from a file:
* remember to:
#include <fstream>
* declare an ifstream object:
ifstream in_stream;
* to read from a file,
you need to connect the ifstream object TO
that file --
you can do that using the ifstream class'
open method.
* one version of open expects a string
representing the file name (relative to
the current working directory),
and it attempts to attach that ifstream
object TO that file for reading;
in_stream.open("infile.txt");
* now this can be used similarly to how we use
cin, to read from that file's characters:
int data;
string next_line;
getline(in_stream, next_line);
in_stream >> data;
* and when you are done reading from the file,
it is CS 112 CLASS STYLE to explicitly close
the stream with the close method, which expects
no arguments:
in_stream.close();
* how about file output?
* remember to:
#include <fstream>
* declare an ofstream object
ofstream out_stream;
* ofstream has more than one overloaded
open method --
* if called with one string argument
representing the name of a file
relative to the current working directory,
it tries to create that file if it
does not exist,
and opens that file, DELETING any of
its current contents!!!!!!!!!!!!!!!!!!!
* if called with TWO arguments,
a string representing the name of a file
relative to the current working directory,
and a constant representing whether it should
be opened for appending,
...for appending, this constant is the named
constant app from the ios class,
which you write using the SCOPE RESOLUTION
OPERATOR :: as follows:
ios::app
...and now you'll open for appending if it
exists, and any writing will be done to
the end of the file
* now use this open ofstream to write to this
file as you use cout to write to standard output:
out_stream << "write me to file" << endl
<< (3 + 5) << endl;
* and -- CS 112 CLASS STYLE -- explicitly close this
output file stream when you are done with it,
using its close method:
outstream.close();
======
review/overview of C++ arrays
======
* in C++, an array is one of the lowest-level
built-in data structures
* it is a collection of values (with one name)
* its values are stored consecutively in memory
* all of its values are of the SAME type
* you must give its size when it is created
* restrictive, but EFFICIENT!
* first element has index 0
last element has index (size - 1)
* array syntax to declare a statically-allocated
LOCAL VARIABLE array:
elem_type arr_name[num_of_elements];
Optionally, you can initialize it using
curly braces:
elem_type arr_name[num_of_elements] = {expr1, expr2, ... exprn};
* to access ONE element of an array,
arr_name[desired_index]
string words[5] = {"apple", "grape", "lemon", "cherry",
"chocolate"};
cout << words[0] << endl; // prints apple
cout << words[4] << endl; // prints chocolate
words[2] = "lime"; // changed the 3rd element,
// lemon, to lime
for loops are LOVELY for arrays!!!!
for (int i = 0; i < 5; i++)
{
cout << words[i] << endl;
}
for (string next_word: words)
{
cout << next_word << endl;
}
* can you have an array parameter in C++?
YES -- C++ views the array (under the hood)
as where it STARTS
* an array parameter does NOT have a size indicated!!!
(because it represents future array arguments,
and each of those WILL have a size when they are
declared...)
...and the array argument is passed as the address
of where the array starts in memory...!
ret_type arr_funct(..., arr_type arr_param[], int arr_size,
...)
* note that to indicate an expected
array in a function signature,
we'll use the notation:
; signature: arr_funct: .... arr_type[] int ... -> ret_type