Title: Stream Processing and stream operations for IO Input Output
1Stream Processing and stream operations for I/O
(Input / Output)
- Well see section 6.3 and 6.4 (some parts
excluded) - Different methods of word and number input
processing - simple application counting words of
- an input stream (most famous example is cin)
- a text file
- different characteristics of cin (mostly not in
the book) - section 9.1, 9.2 (and maybe 9.3 in recitations)
- input and output operations on streams
- reading a full line into a string
- reading character by character
- Some stuff is not in the book.
2Whats an I/O (Input/Output) Stream?
- A sequence of characters flowing between the I/O
devices and programs - therefore it is a buffer area for I/O
- We extract and insert using gtgt and ltlt operators
respectively - cin is the standard input stream (i.e. keyboard)
- cout is the standard output stream (i.e. monitor)
- We also have file streams (for file I/O) that we
will see later
Ali Veli 45 Ahmet cs201
gtgt
ltlt
John George 3 Mike Joe
3Counting Words of Keyboard Input
- Problem where to stop processing, if number of
inputs is not known 2 solutions - until a sentinel value
- applicable to number processing as well (have
seen before) - until the end of input stream
- end-of-file indicator (Ctrl-Z in Windows) - we
will see - Sentinel based solution - See sentinel.cpp
- Input and count words until the sentinel value
(in this example, "end") is entered. - Input may be entered in different lines
- the sentinel value ("end") should not happen in
the text of which you want to count words - otherwise you do not count the rest
- can be applied to counting numbers as well
- without any change
- just enter numbers instead of words
- But it does not check whether the entered value
is a valid number or not
4Counting Words of Keyboard Input
- Non-sentinel version - See countw.cpp
- string word
- int numWords 0
- while (cin gtgt word)
-
- numWords
-
- cout ltlt "number of words read "ltlt numWords ltlt
endl - Process until the end of stream
- Input may be entered in different lines
- end of stream is specified by end-of-file
character Ctrl-Z - Type Ctrl-Z (press Ctrl first and while it is
pressed, type Z) as the first character on a new
line when the program runs and waits for more
words. That signals the end of input. - you may need to press the enter key twice in
Windows - Similar solutions can be applied to integer/real
processing as well - we will give examples later
read one word and check if successfully read
5Detailed Examination of cin
- The statement cin gtgt variable
- reads the value of variable, and
- returns the remaining stream for other data input
- cin gtgt num1 gtgt str gtgt mydouble actually
works as (((cin gtgt num1) gtgt str) gtgt mydouble) - Although it may seem strange, cin gtgt variable
also returns a boolean value depending on the
success of the input operation - true, if input operation is successful
- if there was data and data was of the correct
type - type is not a problem for strings since every
input is string - but for numeric data, type is a problem
- false, if there is no data (end of file is
reached) or data is of wrong type
6sum10nums.cpp revisited (not in book)
- Can we check whether the input is a valid integer
before adding up? - Yes, see sum10validnums.cpp (not in book)
- int num, sum, count
- sum 0 // initialize sum
- cout ltlt "Please enter 10 integers to add up "
- for (count1 count lt 10 count)
-
- if (cin gtgt num)
-
- cout ltlt num ltlt " is a valid entry" ltlt endl
- sum num // add it to the sum if valid
-
- else // else display a message
-
- cout ltlt "entry " ltltcountltlt"is invalid"ltlt
endl -
-
- cout ltlt "the sum is " ltlt sum ltlt endl
read the next number and checks if it is a valid
integer
7sum10nums.cpp revisited (not in book)
- This solution works for valid numbers
- but does not work for invalid numbers as intended
- actually does not read inputs after the first
invalid entry - reason is that once an invalid input is detected,
some error flags are set automatically, and while
they are set, cin does not work - in the program you may clear the error flags by
cin.clear() - however you have to skip the invalid entry since
the invalid entry is still in the stream, but
how? - There is no particular stream member function to
skip the next data on the stream. You can skip
data on stream by just reading it. - Trying to read into an integer variable does not
help either. - A possible solution is to read it into a string
variable. Every word can be read into strings. - See sum10validnumsfixed.cpp (not in book) for the
fixed program - next slide
8sum10nums.cpp revisited (not in book)
- int num, sum, count string s
- sum 0 //initialize sum
- cout ltlt "Please enter 10 integers to add up "
- for (count1 count lt 10 count)
-
- if (cin gtgt num)
-
- cout ltlt num ltlt " is a valid entry" ltlt
endl - sum num // add it to the sum if valid
-
- else
-
- cin.clear()
- cin gtgt s
- cout ltlt "entry " ltlt count ltlt "is
invalid ltltendl -
-
read the next number and checks if it is a valid
integer
if input is not valid, clear the error flags
and skip the invalid entry
9Finding Min/Max of input numbers
- Iterative search of all candidates
- if the current candidate is smaller/larger the
min/max so far, then set the candidate as current
min/max - what is going to be the initial value of current
min (or max)? - for min initialize to the possible maximum value
- for max initialize to the possible minimum value
- Reason is to make the first input current min
(max) - Largest int and double values are found in
ltclimitsgt and ltcfloatgt, respectively (or
ltlimits.hgt and ltfloat.hgt) - youll need to include them
- INT_MAX and INT_MIN are max and min int values
- DBL_MAX and DBL_MIN are max and min double values
- What happens if all input numbers are INT_MAX
(INT_MIN)? - no problem
10Example
- Find min of input values until end-of-file or
until an invalid input is entered - see mindatainput.cpp (not in the book as is)
- Study yourselves
- Modify the program to discard invalid integers
- Find max
- repeat for double values
11Streams for reading and writing files
- Weve seen the standard input stream cin, and the
standard output stream cout - For reading from the keyboard, writing to the
screen - Accessible from ltiostreamgt
- Other streams let us read from files and write to
files - Why do we need such a file I/O?
- Because files are permanently stored whereas the
keyboard entry is for one time and screen output
is volatile - We can input the same data several times if we
use file input - Or we can modify the input file and re-run
program using the modified data - We can save the output for future reference
12Streams for reading and writing files
- syntax for reading and writing is similar to cin
and cout, because they are all streams - To use a file stream, it must be opened first
- Opening binds the stream variable to a physical
file - After opening, I/O to/from this file can be
performed. - Should close file streams, but happens
automatically for input streams when the program
finishes. - We need to close output streams as will be
discussed later - cin and cout are not opened and closed, because
they are standard streams and compiler knows how
to handle them - Input files are generally text files that can
easily be generated using Notepad.
13Input file stream Note similarity to cin
- string word
- int numWords 0 // words read so far
- while (cin gtgt word) // while read succeeded
read and - numWords // count
-
- cout ltlt "number of words read " ltlt numWords ltlt
endl - string word
- int numWords 0
- ifstream input // defining input file
stream - string filename
- cin gtgt filename // input the file
name - input.open(filename.c_str()) // open the file
-
- while (input gtgt word) // while read succeeded
from file - numWords
-
counting words from keyboard
counting words of a file
14Counting words in a file
- See countw2.cpp
- Idea is in the previous slide
- Enhancement also finds the average word length
- add the word lengths up and at the end divide it
by the word count - Study for yourselves
- find the largest and smallest word in a file
15Example (not in book)
- Find the longest word (max number of characters)
in a file. - Idea for algorithm/program
- Read every word, remember the longest word read
so far - Each time a word is read, compare to
longest-so-far. If longer, then theres a new
longest-so-far - See longestword.cpp (not in book)
- why did we initialize maxlength to 0?
- zero is the minimum possible word length
- initialization to any negative value would also
work - what happens if there are more than one words
with the same max length in the file? - finds the first one (finding all either requires
processing the same file twice or requires some
other tools that we will learn in a few weeks)