Professional Documents
Culture Documents
Lecture 13 - IO Stream
Lecture 13 - IO Stream
IO stream
Concordia University
11/15/2022 2
What we learn in Lecture 13
• IO stream class
• Istream
• Ostream
• fstream
• Reading a file
• Writing into a file
• Copying a file
11/15/2022 3
I/O Streams
• C++ standard libraries provide an extensive set of I/O capabilities
• C++ I/O occurs in streams
• A stream represents a sequence of data (in bytes)
• I/O operations from devices to memory and vice versa are often
bottleneck
• Wait for disk drive/keyboard input
• Low-level I/O capability
• Unformatted (not convenient for programmers)
• Byte-by-byte transfer
• High-speed, high-volume transfers
• High-level I/O capability
• Formatted
• Bytes grouped (into integers, characters, strings, etc.)
• Suitable for most I/O needs other than high-volume transfers
4
I/O Streams (cont.)
• iostream library
• Has header files with hundreds of I/O capabilities
• Declares basic services
• Contains definition of two data types
• istream: input stream that enables char input
• Common input variable (cin)
• ostream: output stream that enables char output
• Common output variable (cout)
• Unbuffered/buffered common error streams (cerr/clog)
• <iomanip>
• Formatted I/O with parameterized stream manipulators
• Example: fixed, setprecision
• <fstream>
• File processing operations (a.k.a. disk file IO)
5
Stream Classes and Objects
• #<iostream> means:
• cin
• cout
• cerr: is the unbuffered standard error stream which is used to output
the errors
• Clog: is the buffered standard error stream that is used to output the
errors.
6
Stream Classes and Objects
8
Input Stream
• End-of-file
• Indicates end of input
• ctrl-z on IBM-PCs
cin.eof()
• Returns 1 (true) if EOF has occurred
9
Input Stream (cont.)
10
Input Stream (cont.)
• get(charRef)
11
Input Stream (cont.)
Reference Argument
12
Input Stream (cont.)
13
Input Stream (cont.)
14
Input Stream (cont.)
Reads 7 char
15
getline Member Function
• Two syntaxes:
16
getline Member Function (cont.)
Enter a sentence:
Using the getline member function
17
getline Member Function (another
format)
Enter a sentence:
Using the getline member function
18
getline() Vs. cin>>
19
Output Stream
20
Character Output
• May be cascaded
cout.put('A').put('\n'); cout << “A” << “\n”;
21
Character Output
The same
22
Character Output
correct
wrong
23
Character Output
24
Character Output
25
get and put
26
Files in C++
• A file is a collection of data stored on a disk
• C++ views a file simply as a sequence of bytes
• Each file ends with a special end of file marker
27
Creating Files in C++
28
Opening a File for I/O (cont.)
• fail() function
• Returns true, nonzero, if file fails to open
• Note you can also specify the whole path of the file
• E.g., C:\\MyDir\\myfile.txt
29
Closing a File
30
Example 1: Saving student grades
into a file
31
Example 2 Reading student grades
from a file
32
Example 3 Making a copy of a file
33
Example 3 Making a copy of a file
(cont.)
34
Example: I/O stream
Enter a sentence:
Contrasting string input with cin and cin.get