CMSC 202 - PowerPoint PPT Presentation

About This Presentation
Title:

CMSC 202

Description:

names.at(1) = 'Flintstone'; for (int i = 0; i = names.size ... Characters that come OUT of your program. Input/Output (Sources/Sinks) Input: Keyboard, File, ... – PowerPoint PPT presentation

Number of Views:75
Avg rating:3.0/5.0
Slides: 24
Provided by: danawo
Category:

less

Transcript and Presenter's Notes

Title: CMSC 202


1
CMSC 202
  • Streams

2
Warmup
  • What happens in the following?
  • vectorltstringgt names
  • names.at(0) Fred
  • names.at(1) Flintstone
  • for (int i 0 i lt names.size() i)
  • cout ltlt names.at(i) ltlt
  • cout ltlt endl

3
What is a stream?
  • Flow of characters (or other data)
  • Input
  • Characters that come IN to your program
  • Output
  • Characters that come OUT of your program
  • Input/Output (Sources/Sinks)
  • Input Keyboard, File,
  • Output Monitor, File,

4
Standard In/Out
  • Standard in
  • cin
  • Standard out
  • cout
  • Why are these interesting?
  • Under any OS these can be redirect
  • You can treat a file as cin
  • You can treat a file as cout

5
Error Stream
  • cerr
  • Standard error stream
  • NOT redirected when cout is redirected
  • Use exactly like cout
  • Example
  • if (denominator 0)
  • cerr ltlt Fatal error denominator 0 ltlt
    endl
  • exit(1)

6
File I/O
  • File streams work EXACTLY like cin/cout
  • More setup is required
  • Open a file
  • Read from/Write to a file
  • Close the file
  • Library
  • include ltfstreamgt

7
File Input
  • ifstream
  • Datatype for file input
  • Opening an input file
  • ifstream fin(filename)
  • Or
  • ifstream fin
  • fin.open(filename)
  • Reading from an input file (you can read any
    primitive!)
  • char a
  • fin gtgt a
  • Closing an input file
  • fin.close()

8
File Input - Example
  • Read a list of last names from a file
  • ifstream fin(names.txt)
  • vectorltstringgt names
  • string name
  • while (fin gtgt name)
  • names.push_back(name)

Whats happening here?
9
File Output
  • ofstream
  • Datatype for file output
  • Opening an output file
  • ofstream fout(filename)
  • Or
  • ofstream fout
  • fout.open(filename)
  • Writing a char to an output file
  • char a A
  • fout ltlt a
  • Closing an output file
  • fout.close()

10
File Output - Example
  • Write a list of names to a file
  • ofstream fout(names.txt)
  • vectorltstringgt names
  • string name
  • // Assume vector gets values
  • for (int i 0 i lt names.size() i)
  • fout ltlt namesi ltlt endl

11
File Streams - 1 Issue
  • Streams expect a C-string as its parameter
  • Example
  • string inFilename input.txt
  • ifstream fin( inFilename.c_str() )
  • string outFilename output.txt
  • ofstream fout( outFilename.c_str() )
  • What does c_str() do?
  • Look back at the string material!

12
Practice
  • Open a file for input called phones.txt
  • File has a name (string) followed by a number
    (int)
  • Read in the data from the file
  • Close the input file
  • Open a file for output called reverse.txt
  • Print the number followed by the name to the file
  • Close the output file

13
Input Streams Pitfall!
  • Mixing getline() and gtgt can be bad!
  • gtgt
  • Skips all leading whitespace
  • Leaves trailing whitespace for next extraction
  • Leaves the \n character
  • getline
  • Retrieves until end of line character
  • Removes the \n character, but does not store it

14
Input Streams Pitfall!
  • Example
  • int age
  • string name
  • cout ltlt "Input your age and first name"
  • cin gtgt age
  • getline( cin, name )
  • User types
  • 42
  • Bob Smith
  • Solution?
  • cin.ignore() // discard a single character
  • cin.ignore(1000, \n) // discard up to 1000
    chars, stopping at \n

age 42 name
15
Checking for end-of-file
  • End of file (fin) or End of input (cin)
  • 3 strategies
  • while (!fin.eof()) // Best
  • while (fin gtgt variable) // Pretty good
  • while (fin) // NOT preferred
  • .eof()
  • Returns true if EOF character has been seen

16
Formatting Output
  • Setf set formatting flags
  • outStream.setf( iosfixed)
  • Floating point values have a decimal point
  • outStream.setf( iosshowpoint)
  • Floating point values have trailing zeros
  • outStream.setf( iosright)
  • Output is right-justified in output field
  • outStream.setf( iosleft)
  • Output is left-justified in output field
  • Setf stays in effect until reset

17
Formatting Output
  • outStream.precision( int places )
  • Sets the number of places to the right of the
    decimal
  • Stays in effect until next call to precision()
  • outStream.width( int size )
  • Sets minimum number of spaces to use to output
    the NEXT item
  • Only works on ONE item at a time
  • HINT great for aligning tabular output!

18
Manipulators
  • Library
  • include ltiomanipgt
  • setprecision( int places )
  • Same as outStream.precision( int places )
  • outStream ltlt setprecision(2) ltlt money ltlt endl
  • setw( int size )
  • Same as outStream.width( int size )
  • outStream ltlt setw(10) ltlt Name ltlt endl
  • fixed
  • Same as setf( iosfixed )
  • outStream ltlt fixed ltlt money ltlt endl
  • showpoint
  • Same as setf( iosshowpoint )
  • outStream ltlt showpoint ltlt money ltlt endl

19
Formatting Output Pitfall!
  • Recall
  • Most manipulators stay on until reset
  • Issue?
  • Function that modifies these stream flags
  • Solution?
  • Save the current state
  • int savePrecision outStream.precision()
  • int saveFlags outStream.flags()
  • Reset the current state
  • outStream.precision( savePrecision )
  • outStream.flags( saveFlags )

20
String Streams
  • ostringstream
  • Format messages and treat like a string
  • Example
  • string FormatMessage( int age )
  • ostringstream msg
  • msg ltlt "\nThis is my message\n"
  • msg ltlt "John is " ltlt setw(6) ltlt age
  • ltlt " years old\n"
  • // use the str() function to access the
  • // C-string in the msg
  • return msg.str( )

21
String Streams
  • istringstream
  • Parse messages based on whitespace
  • Example
  • void PrintWords( string stringToPrint)
  • // create and initialize the istringstream
  • // from the string
  • istringstream inStream( stringToPrint )
  • string word
  • while ( inStream gtgt word )
  • cout ltlt word ltlt endl

22
Practice
  • Use setf, setw, fixed, showpoint, and
    setprecision to do the following
  • Ask the user for 2 names and salaries
  • Print them formatted like this
  • Name Salary
  • --------- ------------
  • John Doe 43523.00
  • Jane Donner 3129.97

23
Challenge
  • Use vectors, files and strings to
  • Part 1
  • Read in an unknown number of paired values from a
    file (Name - string, Phone Number - integer)
  • Part 2
  • Print the collection to another file
  • Align the output vertically
  • Format the phone number using dashes
  • HINT think about using modulus and integer
    division!
Write a Comment
User Comments (0)
About PowerShow.com