Download C++ Stream Input/Output: Streams, Formatted and Unformatted I/O and more Slides Computer Science in PDF only on Docsity!
Chapter 21 - C++ Stream Input/Output Basics
Outline
21.1 Introduction
21.2 Streams
21.2.1 Iostream Library Header Files
21.2.2 Stream Input/Output Classes and Objects
21.3 Stream Output
21.3.1 Stream-Insertion Operator
21.3.2 Cascading Stream-Insertion/Extraction Operators
21.3.3 Output of char * Variables
21.3.4 Character Output with Member Function put ; Cascading put s
21.4 Stream Input
21.4.1 Stream-Extraction Operator
21.4.2 get and getline Member Functions
21.4.3 istream Member Functions peek , putback and ignore
21.4.4 Type-Safe I/O
21.5 Unformatted I/O with read , gcount and write
21.1 Introduction
⢠Many C++ I/O features are object-oriented
ā Use references, function overloading and operator
overloading
⢠C++ uses type safe I/O
ā Each I/O operation is automatically performed in a
manner sensitive to the data type
⢠Extensibility
ā Users may specify I/O of user-defined types as well as
standard types
21.2 Streams (II)
- I/O operations are a bottleneck
- The time for a stream to flow is many times larger than the time it
takes the CPU to process the data in the stream
- Low-level I/O
- Unformatted
- Individual byte unit of interest
- High speed, high volume, but inconvenient for people
- High-level I/O
- Formatted
- Bytes grouped into meaningful units: integers, characters, etc.
- Good for all I/O except high-volume file processing
21.2.1 Iostream Library Header Files
- iostream library:
- : Contains cin, cout, cerr, and
clog objects
- : Contains parameterized stream
manipulators
- : Contains information important to user-
controlled file processing operations
21.2.2 Stream Input/Output Classes and Objects (II)
cin >> someVariable;
- cin knows what type of data is to be assigned to
someVariable (based on the type of someVariable).
- ostream: output streams
- cout << someVariable;
- cout knows the type of data to output
- cerr << someString;
- Unbuffered - prints someString immediately.
- clog << someString;
- Buffered - prints someString as soon as output buffer is full
or flushed
21.3 Stream Output
- ostream: performs formatted and unformatted
output
- Uses put for characters and write for unformatted characters
- Output of numbers in decimal, octal and hexadecimal
- Varying precision for floating points
- Formatted text outputs
21.3.2 Cascading Stream-Insertion/Extraction
Operators
- << : Associates from left to right, and returns a
reference to its left-operand object (i.e. cout).
cout << "How" << " are" << " you?";
Make sure to use parenthesis:
cout << "1 + 2 = " << (1 + 2);
NOT
cout << "1 + 2 = " << 1 + 2;
21.3.3 Output of char * Variables
- << will output a variable of type char * as a
string
- To output the address of the first character of that
string, cast the variable as type void *
21.3.4 Character Output with Member Function
put ; Cascading put s
- put member function
- Outputs one character to specified stream
cout.put( 'A');
- Returns a reference to the object that called it, so may be
cascaded
cout.put( 'A' ).put( '\n' );
- May be called with an ASCII-valued expression
cout.put( 65 );
21.4 Stream Input
(stream-extraction) - Used to perform stream input - Normally ignores whitespaces (spaces, tabs, newlines) - Returns zero (false) when EOF is encountered, otherwise
returns reference to the object from which it was invoked (i.e.
cin)
- This enables cascaded input
cin >> x >> y;
controls the state bits of the stream - failbit set if wrong type of data input - badbit set if the operation fails
1. Initialize variables
2. Perform loop
3. Output
Program Output
1 // Fig. 21.11: fig21_11.cpp 2 // Stream-extraction operator returning false on end-of-file. 3 #include 4 5 using std::cout; 6 using std::cin; 7 using std::endl; 8 9 int main()
10 {
11 int grade, highestGrade = -1;
12
13 cout << "Enter grade (enter end-of-file to end): ";
14 while ( cin >> grade ) {
15 if ( grade > highestGrade )
16 highestGrade = grade;
17
18 cout << "Enter grade (enter end-of-file to end): ";
19 }
20
21 cout << "\n\nHighest grade is: " << highestGrade << endl;
22 return 0;
23 }
Enter grade (enter end-of-file to end): 67 Enter grade (enter end-of-file to end): 87 Enter grade (enter end-of-file to end): 73 Enter grade (enter end-of-file to end): 95 Enter grade (enter end-of-file to end): 34 Enter grade (enter end-of-file to end): 99 Enter grade (enter end-of-file to end): ^Z
Highest grade is: 99 Docsity.com
21.4.2 get and getline Member Functions
- cin.get(): inputs a character from stream
(even white spaces) and returns it
- cin.get( c ): inputs a character from
stream and stores it in c
1. Initialize variables
2. Input data
2.1 Function call
3. Output
Program Output
1 // Fig. 21.12: fig21_12.cpp 2 // Using member functions get, put and eof. 3 #include 4 5 using std::cout; 6 using std::cin; 7 using std::endl; 8 9 int main() 10 { 11 char c; 12 13 cout << "Before input, cin.eof() is " << cin.eof() 14 << "\nEnter a sentence followed by end-of-file:\n"; 15 16 while ( ( c = cin.get() ) != EOF ) 17 cout.put( c ); 18 19 cout << "\nEOF in this system is: " << c; 20 cout << "\nAfter input, cin.eof() is " << cin.eof() << endl; 21 return 0; 22 }
Before input, cin.eof() is 0 Enter a sentence followed by end-of-file: Testing the get and put member functions^Z Testing the get and put member functions EOF in this system is: - After input cin.eof() is 1
1. Initialize variables
2. Input
2.1 Function call
3. Output
Program Output
1 // Fig. 21.14: fig21_14.cpp 2 // Character input with member function getline. 3 #include 4 5 using std::cout; 6 using std::cin; 7 using std::endl; 8 9 int main()
10 {
11 const SIZE = 80;
12 char buffer[ SIZE ];
13
14 cout << "Enter a sentence:\n";
15 cin.getline( buffer, SIZE );
16
17 cout << "\nThe sentence entered is:\n" << buffer << endl;
18 return 0;
19 }
Enter a sentence: Using the getline member function
The sentence entered is: Using the getline member function