
















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of c++ stream input/output, focusing on streams, formatted and unformatted i/o. It covers the use of iostream library header files, stream classes and objects, stream output using put and write functions, and stream input using get and getline functions. The document also discusses low-level and high-level i/o, type-safe i/o, and unformatted i/o using read, gcount, and write member functions.
Typology: Slides
1 / 24
This page cannot be seen from the preview
Don't miss anything!

















21.2 Streams (II)
takes the CPU to process the data in the stream
21.2.1 Iostream Library Header Files
clog objects
manipulators
controlled file processing operations
21.2.2 Stream Input/Output Classes and Objects (II)
cin >> someVariable;
someVariable (based on the type of someVariable).
or flushed
21.3 Stream Output
output
21.3.2 Cascading Stream-Insertion/Extraction
Operators
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
string
string, cast the variable as type void *
21.3.4 Character Output with Member Function
put ; Cascading put s
cout.put( 'A');
cascaded
cout.put( 'A' ).put( '\n' );
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)
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 // Fig. 21.11: fig21_11.cpp 2 // Stream-extraction operator returning false on end-of-file. 3 #include
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
21.4.2 get and getline Member Functions
(even white spaces) and returns it
stream and stores it in c
1 // Fig. 21.12: fig21_12.cpp 2 // Using member functions get, put and eof. 3 #include
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 // Fig. 21.14: fig21_14.cpp 2 // Character input with member function getline. 3 #include
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