Tải bản đầy đủ (.pdf) (16 trang)

Lecture Programming in C++ - Chapter 4: Basic input and output

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (460.22 KB, 16 trang )

Chapter 4 – Basic Input and 
Output


Reading Data 
Keyboard input
– cin object
– >>  called extraction operator

Syntax:
cin >> variable;
Example:
cin >> income >> expense;
Reads to values typed at keyboard (separated by whitespace)
     to memory locations names, income, and expense
Lesson 4.1


Prompt
Message sent from program to screen
Informs user what data to enter
Precedes cin statement in program
int age;
cout << “Please enter your age.”<< endl;
cin >> age;
Lesson 4.1


Input 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


Writing Output to a File
Similar 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


Opening 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”
Two backslashes needed – escape sequence for one
Double quotes surround file_name
Lesson 4.2



Writing to Files
General 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


Closing 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


Open File for Reading
Need fstream header
          #include <fstream>
Declare object of ifstream
      ifstream object_name(“file_name”);
Use ifstream object to read file like cin

Lesson 4.3



Reading From a File
Use 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


Reading From a File
money.dat
35  12
2  3.5   6.18   34

infile >> s1 >> s2 >> s3;
infile >> s4;

s1
35

s2
12

s3
2

s4
3.5

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


Reading Character Data
Can 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


Examples:
char c1,c2,c3,c4,c5,c6,c7,c8;
cin >> c1 >> c2 >> c3;
cin >> c4 >> c5;
cin >> c6;
cin >> c7 >> c8;
c1 a
c5 2
c2 f
c6 h

c3 d
c7 j
c4 3
c8 w
Lesson 4.4

Keyboard Input
af  d32(enter)
hj(tab)w (enter)
k
Note: All whitespace
characters are ignored!


Input 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


Input Buffer
Keyboard entry:  

af  d32(enter)
hj(tab)w (enter)


 a   f       d  3   2  (enter)  h   j  (tab) w (enter)

cin >> c1 >> c2 >> c3 >> c4 >> c5 >> c6;
 a
f
d
3
2
h
Lesson 4.4


Summary
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



×