Lecture Programming in C++ - Chapter 4: Basic input and output. On completion of this chapter students will 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.
Trang 1Output
Trang 2Keyboard input
– cin object
– >> called extraction operator
Syntax: cin >> variable;
Example:
Lesson 4.1
cin >> income >> expense;
Reads to values typed at keyboard (separated by whitespace)
to memory locations names, income, and expense
Trang 3Message sent from program to screen
Informs user what data to enter
Precedes cin statement in program
Lesson 4.1
int age;
cout << “Please enter your age.”<< endl; cin >> age;
Trang 4Series 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
Trang 5Similar to writing to screen
Use object connected to output file
Need the fstream header
#include <fstream>
Open file for writing
– Declare object of ofstream class
Lesson 4.2
Trang 6General 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
Double quotes surround file_name Two backslashes needed – escape sequence for one
Trang 7General form
object_name << variable_name;
Use ofstream object to write to file like cout was used
ofstream outfile(“C:\\salary.out”);
outfile << “Salary for week was ” << money;
Additional writing appended to file
Lesson 4.2
Trang 8General 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
Trang 9Need fstream header
#include <fstream>
Declare object of ifstream
ifstream object_name(“file_name”);
Use ifstream object to read file like cin
Lesson 4.3
Trang 10Use ifstream object to read file like cin used for keyboard
ifstream infile(“C:\\money.dat”);
infile >> salary1 >> salary2;
C++ looks for whitespace between numbers
– Newline character treated as whitespace
Additional reading continues sequentially
Lesson 4.3
Trang 11Lesson 4.3
35 12
2 3.5 6.18 34
money.dat
infile >> s1 >> s2 >> s3;
s4 3.5 infile >> s4;
Note: The number of data entries and their data types of variables should read should match the number and order within
the file!
Trang 12Can use cin
cin >> c1 >> c2 >> c3;
Reads next three characters typed at
keyboard
Whitespace is NOT needed for separating character data
– if used, whitespace is ignored
Lesson 4.4
Trang 13Lesson 4.4
cin >> c1 >> c2 >> c3;
cin >> c4 >> c5;
cin >> c6;
cin >> c7 >> c8;
af d32(enter)
k
Keyboard Input
c1
c2
c3
c4
c5 c6 c7 c8
a f d 3
2 h j w
char c1,c2,c3,c4,c5,c6,c7,c8;
Note: All whitespace characters are ignored!
Trang 14Region 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
Trang 15Lesson 4.4
Keyboard entry: af d32(enter)
a f d 3 2 (enter) h j (tab) w (enter)
cin >> c1 >> c2 >> c3 >> c4 >> c5 >> c6;
Trang 16Read input from keyboard
Write output to file
Read data from file
Read and work with character data Work with the input buffer
Learned how to: