Title: Chapter 4 Basic Input and Output
1Chapter 4 Basic Input and Output
2Reading Data
- Keyboard input
- cin object
- gtgt called extraction operator
- Syntax cin gtgt variable
- Example
cin gtgt income gtgt expense
Reads to values typed at keyboard (separated by
whitespace) to memory locations names,
income, and expense
Lesson 4.1
3Prompt
- Message sent from program to screen
- Informs user what data to enter
- Precedes cin statement in program
int age cout ltlt Please enter your age.ltlt
endl cin gtgt age
Lesson 4.1
4Input and Output Streams
- Series of bytes in sequence
- Flow from device to device
- Objects are regions of storage in memory
- Object used determines device stream comes from
or goes to - cin and cout are identifiers for objects defined
by iostream (screen and keyboard)
Lesson 4.1
5Writing Output to a File
- Similar to writing to screen
- Use object connected to output file
- Need the fstream header
- include ltfstreamgt
- Open file for writing
- Declare object of ofstream class
Lesson 4.2
6Opening Files
- General form
- ofstream object_name (file_name)
- Choose object_name like variable name
- object_name is object of class ofstream
- Filename is where output will be stored
- Can use full pathname C\\salary.out
Lesson 4.2
7Writing to Files
- General form
- object_name ltlt variable_name
- Use ofstream object to write to file like cout
was used - ofstream outfile(C\\salary.out)
- outfile ltlt Salary for week was ltlt money
- Additional writing appended to file
Lesson 4.2
8Closing Files (input and output)
- General form
- object_name.close ( )
- Use C library function close
- Use both object and function name separated by a
period - Example outfile.close( )
Lesson 4.2
9Open File for Reading
- Need fstream header
- include ltfstreamgt
- Declare object of ifstream
- ifstream object_name(file_name)
- Use ifstream object to read file like cin
Lesson 4.3
10Reading From a File
- Use ifstream object to read file like cin used
for keyboard - ifstream infile(C\\money.dat)
- infile gtgt salary1 gtgt salary2
- C looks for whitespace between numbers
- Newline character treated as whitespace
- Additional reading continues sequentially
Lesson 4.3
11Reading From a File
s1
s2
s3
money.dat
35 12 2 3.5 6.18 34
12
2
35
infile gtgt s1 gtgt s2 gtgt s3
3.5
infile gtgt s4
Note The number of data entries and their data
types of variables should read should
match the number and order within the
file!
Lesson 4.3
12Reading Character Data
- Can use cin
- cin gtgt c1 gtgt c2 gtgt c3
- Reads next three characters typed at keyboard
- Whitespace is NOT needed for separating character
data - if used, whitespace is ignored
Lesson 4.4
13Examples
Keyboard Input
char c1,c2,c3,c4,c5,c6,c7,c8
cin gtgt c1 gtgt c2 gtgt c3 cin gtgt c4 gtgt c5 cin gtgt
c6 cin gtgt c7 gtgt c8
af d32(enter)
hj(tab)w (enter)
k
a
2
Note All whitespacecharacters are ignored!
f
h
d
j
3
w
Lesson 4.4
14Input Buffer
- Region of memory used for temporary storage of
information being transferred between devices - Accessed sequentially
- Position indicator keeps track of point to which
information has been read
Lesson 4.4
15Input Buffer
Keyboard entry
af d32(enter)
hj(tab)w (enter)
a f d 3 2 (enter)
h j (tab) w (enter)
cin gtgt c1 gtgt c2 gtgt c3 gtgt c4 gtgt c5 gtgt c6
a
f
d
3
2
h
Lesson 4.4
16Summary
Learned how to
- Read input from keyboard
- Write output to file
- Read data from file
- Read and work with character data
- Work with the input buffer