CS Week 2 - Streams Flashcards
ostream
output stream
a class that supports output
available via iostream and in namespace std
insertion operator
«_space;
for converting different types of data into a sequence of characters, that sequence is normally placed into a buffer, and the system then outputs the buffer at various times
cout
a predefined ostream object that is pre-associated with a system’s standard output, usually a computer screen
how the ostream «_space;works
- the insertion operator converts the string literal to characters, temporarily storing characters in an output buffer
- the system then writes the buffer’s content to the screen
istream
input stream
a class that supports input
available via iostream
extraction operator
> > to extract data from a buffer and write the data into different types of variables
cin
a predefined istream pre-associated with a system’s standard input, usually a keyboard
how istream»_space; works
- the system automatically puts the standard input into a data buffer associated with cin
- > > operator skips leading whitespace and extacts as many characters as possible consistent with the target variable’s type
- the operator then stops at the next whitespace
- converts the extracted characters to the target variable’s type and stores the result into the variable
to read file input
include <fstream></fstream>
can create a new input stream that comes from a file
ifstream inFS;
inFS.open(str);
if(!inFS.is_open()){
cout «_space;“error opening”; return 1;}
inFS»_space; aVar;
inFS.close();
when would we use cin, cout, ifstream, ofstream, istringstream, ostringstream
cin - input from the keyboard
cout - output to the screen
ifstream - input from a file
ofstream - write to a file
istringstream - read input from a string
ostringstream - insert characters into a string buffer (instead of screen)
reading to the end of the file
while (inFS»_space; aVar) {
//do something with aVar;
}
inFS.eof();
returns true if the previous stream operation reached the end of the file
inFS.fail();
returns true if the previous stream operation had an error
- instead of this use while (inFS»_space; aVar){ /do something/ }
stream error
occurs when insertion or extraction fails causing the stream to enter an error state
- wrong type, too large, too small
- may skips extraction, set the var to 0, or set the var to the max or min of the type
inFS.clear();
inFS.good();
inFS.eof();
inFS.fail();
inFS.bad();
and when to use
1-bit error flags
clear - stream’s error state is cleared
good- returns true if no stream errors have occurred
eof - returns value of eofbit, if end of file reached on extraction
fail - returns true if either failbit or badbit is set, indicating an error for the previous stream operation
bad - returns true id badbit is set. indicating the stream is bad
- use if you want to know what went wrong
how to write to a file
include <fstream></fstream>
declare a variable of type ofstream to write to a file
ofstream outFS;
outFS.open(fileName);
if(!outFS.is_open()) {
//output error
}
outFS «_space;“Hiya!! «_space;endl;
outFS.close();
how to ask if the right value was extracted
if (!cin) {
cout «_space;“bad input” «_space;endl;
}
how to use input string stream
include <sstream></sstream>
read input from an associated string
istringstream inSS (strToUse);
string firstName;
string lastName;
inSS»_space; firstName;
inSS»_space; lastName;
istringstream inSS;
string lineStr;
string data;
cout «_space;“Enter names separated by spaces” «_space;endl;
getline(cin, lineStr);
inSS.str(lineStr);
while (inSS»_space; data) {
cout «_space;data «_space;endl;
}
.get();
» or «
get - gets one character
> > or «_space;- stops at whitespace
getline(stream, storingVar, stopWhere)
stopWhere - “,” or empty to stop at a new line
getline with string streams
while (getline(cin, lineStr) {
inSS.str(lineStr);
inSS»_space; name;
}
inSS.str();
oSS.str();
.in - copies a string into the string streams input buffer
o- returns the contents of an ostringstream buffer as a string
how to use output string stram
include <sstream></sstream>
ostringstream infoOSS;
cin»_space; aVar;
infoOSS «_space;aVar;
string infoStr = infoOSS.str();
command line arguments
values entered by a user when running a program from a command line
argc
number of command line arguments in argv
- includes the program name itself
int argc
argv
array of strings, one string for each command line argument
argv[0] is the program name
char* argv[]
when you run the program with argc and argv what happens
- system passes an int parameter argc to main(), indicating the number of command line arguments
- system passes a second parameter argv to main(), defined as an array of string, one string for each command line agument
what happens when user does not type in the amount of command lines
extra is ignored
too few cause a problem
- error- accessing elements in argv without first checking argc to ensure the user entered enough arguments, out of range array access
usage message
lists a programs expected command line arguments, program should then return 1, indicating the system that an error occurred
if (argc != the size of argv/a #) {
cout «_space;“Usage: programName input 1…input n” «_space;endl;
return 1;
}
command line arguments are what…
c strings
what converts a string into an integer
in var = atoi(ageStr.c_str());
included in cstdlib
putting quotes arounf an arument in a command line…and how should you access argv elements
allows the string to have any number of spaces
[], no .at
what do command line arguments and files allow you to do
allow user to specify the location of an input or outpur file
argv[#] - # is where that file is located in the command line
inFS.open(argv[1]);
selection sort defifniton
sorting algorithm that treats the input as 2 parts, a sorted part, and an unsorted part and repeatedly selects the proper next value to move from the unsorted to sorted part
selection sort code
function to find location of smallest
int locationOfSmallest (const vector<int> v, int begin, int end) {
int smallest = v.at(begin);
int location = being;</int>
for (i = begin + 1; i < end; ++i) {
if (v.at(i) < smallest) {
location =i; }
}
return location;
}
—————–
code in main
for ( i = 0; i < v.size(); ++i) {
location = locationOfSmallest(v, i, v.size());
swap(v.at(i), v.at(location);
}
runtime of selection sort algorithm
O(N^2)
a list with N elements
outer loop executes N-1 times
inner loop executes an average of N/2 times
if input increases in size by x times, the runtime increases x^2 times
you can use inFS»_space; data in the while conditional when
you know your data is clean
both ways of writing argv
char** argv
char* argv[]