Review for Exam 1

Download Report

Transcript Review for Exam 1

Using Files
Hanly: Chapter 9, some in Chapter 4
Freidman-Koffman: Chapter 8,
iomanip in Chapter 5
CS 117 Spring 2002
3/11/02
• HW 2 due in 2 weeks - start thinking about it
– look at description of P6 for ideas
– program exercises from book
• Program 4
– don't use Money class from book
– printPrice should just print a price in the proper format
- no more
8.1 Standard Input/Output
Streams
• Stream is a sequence of characters
• Working with cin and cout
• Streams convert internal representations to
character streams
• >> input operator (extractor)
• << output operator (inserter)
• Streams have no fixed size
8.2 External Files
• Batch
– Requires use of data files (save to disk)
– Batch can be run during off peak use
– allows things to be complete at start of day
• Interactive
– Real time systems
– Ok for smaller programs
– Programs that complete quickly
Files
• How to attach files to the stream
–
–
–
–
–
–
stream object
external file name
internal name
open
test state
close
Files
• Declare the stream to be processed need to
#include fstream
ifstream ins; // input stream
ofstream outs; // output stream
• Need to open the files
ins.open (inFile);
outs.open (outFile);
Files
• I/O just like using cin and cout
• fail() function - returns true nonzero if
– file fails to open
– input operation fails
• eof() - set to true when program tries to read
beyond the end of the file
– NOT before
• close() - disconnects the file from the stream
variable
File Processing
• Loop processing
– for loops
– while loops
• Newline character
– eof() function returns a False if file is not empty
while ( ! ins.eof())
{
do stuff
}
Reading char and string
Data with >>
• Leading white space skipped
– newline also skipped
do
// pseudocode
ch = next char in stream
while ch is whitespace
• after loop, ch is the char that would be read by >>
• ch is first char of string read by >>
– string ends when next space character is located
Other ways to read
• sometimes want to read white space characters
– get function reads the next character
– getline function reads up to the next newline which is
thrown away
instream.getline( char[], int)
getline( instream, string)
– can specify a different terminator for getline
instream.getline( char[], int, char)
getline( instream, string, char)
8.4 More on Reading String
Data
• Getline - could be used to process an entire
line of data
• Use # as a delimiter character
getline (eds, name, ‘#’);
• Advance the newline
getline (eds, name, ‘\n’);
• Use care when choosing cin, get or getline
Input/Output Manipulators
• setf, unsetf, precision and width
• Can be used with the cout and <<
• #include iomanip when using
• Can be used with any output filestream
I/O Manipulators
setiosflags(ios:: flagname)
set the flag named
flagname
resetiosflags(ios:: flagname) reset the flag to the
default state
setw( n)
set the field width to n
setprecision( n)
use n digits after
decimal point
Formatting with State Flags
• Depending on the setiosflags or
unsetiosflags
– Output can be controlled by other format state
flag
– Flags are enumerated types
• ios::flagname
– boolalpha, fixed, left, right, showpoint etc
ios flags
left
right
justification in field
fixed
fixed point format
scientific
scientific notation
8.6 Common Programming
Errors
• Connecting streams and external files
– Declare stream object and open the file
• Watch use of while loops when processing
– Test values see what you actually have
•
•
•
•
Reading past the eof
White space
Newline character
Formatting via flags
numbers <---> strings
• C++ has stringstreams (sstream library)
– use similar to iostreams
– can get the corresponding string
• C has functions in stdlib.h
– atoi and atof to convert appropriate strings to
numbers
– sscanf works like scanf but uses a char[]
– sprintf is like printf but result is char[]