C++ Unit 3 CHap 2
C++ Unit 3 CHap 2
Stream Concept
• C++ I/O operations use the stream concept.
• The stream refers to a series of bytes or data flow.
• Streams improve performance by managing the flow of data between the main memory
and devices like printers, screens, and network connections.
Output Operation
• Bytes are transferred from the main-memory to an output device (e.g., printer, screen)
during an output operation.
Input Operation
• Bytes flow from an input device (e.g., keyboard) to the main-memory during an
input operation.
Header Files for I/O
• C++ provides predefined functions and declarations through header files to handle
I/O tasks efficiently.
`<iostream>` Header File
• Essential for input/output operations in C++.
• Includes classes like `istream` (input stream) and `ostream` (output stream).
• Commonly used classes:
`cin`: Standard input stream.
`cout`: Standard output stream
Standard Output Stream (`cout`)
• An object of the `ostream` class, connected to the standard output device (typically
a display screen).
• Used with the insertion operator (`<<`), to display output on the console.
Standard Input Stream (`cin`)
• An object of the `istream` class, connected to the standard input device (typically a
keyboard).
• Used with the extraction operator (`>>`), to read input from the console.
Example
#include <iostream>
using namespace std;
int main() {
int age;
cout << "Enter your age: ";
cin >> age;
cout << "Your age is: " << age << endl;
return 0;
}
Output:
Enter your age: 22
Your age is: 22
UNFORMATTED-I/O
Definition: Input- and output-operations where data is read or written without
applying any specific formatting-rules.
The data is transferred in its raw, unmodified form.
Purpose
• To perform simple, direct input- and output-operations without any concern for how
the data is presented or formatted.
• Useful for efficient, low-level data handling where formatting is not necessary.
Usage
• Often used when handling binary-data or performing low-level file operations.
• Provides more control over how data is managed and transferred, without altering its
format.
• Common unformatted-I/O functions are listed in below table:
FORMATTED-I/O
• Definition: Input- and output-operations where data is formatted according to
specific rules or styles before being written to or read from a stream.
Purpose
• To present data in a readable and structured format.
• To ensure data is aligned, padded, or displayed in a particular way according to user
requirements.
Usage
• Formatted-I/O functions are used with output streams (cout) to control how data is
formatted when displayed.
• They can be applied to both numbers and text to ensure consistent and readable
output.
• Common formatted-I/O functions are listed in below table:
• Description: This function sets the number of digits to display after the decimal
point for floating-point numbers in the output stream.
• Parameters:
`int n`: The number of digits to display
• Example Usage:
cout << setprecision(3) << 3.14159; // Outputs: 3.14
setw(int width)
• Description: This function sets the width of the next input/output field. If the data
is shorter than the specified width, it will be padded with spaces or another character
(set by `setfill()`).
• Parameters:
- `int width`: The width of the field for the next input/output operation.
• Example Usage:
cout << setw(10) << 123; // Outputs: " 123" (7 spaces before 123)
setfill(char c)
• Description: This function sets the fill character used to pad fields in the output
stream. It is used in conjunction with `setw()` to pad the output with a specific
character instead of spaces.
• Parameters:
`char c`: The character used to pad the output field.
• Example Usage:
cout << setfill('*') << setw(10) << 1234; // Outputs:"1234" (padded with *)
Example Program: Demonstrating Formatted-I/O
#include <iostream>
#include <iomanip> // For setprecision, setw, and setfill
using namespace std;
int main() {
// Setting precision to 3 digits after the decimal point
cout << setprecision(3) << 3.14159 << endl; // Outputs: 3.14
// Setting width to 10 and displaying the number 123
cout << setw(10) << 123 << endl; // O/Ps: " 123" (7 spaces before 123)
// Setting fill character to '*' and width to 10, then displaying the number 1234
cout << setfill('*') << setw(10) << 1234 << endl; // Outputs: "******1234"
return 0;
}
Output:
3.14
123
******1234
Explanation:
- Precision Example: `setprecision(3)` formats the floating-point number
`3.14159` to show only 3 digits after the decimal point, resulting in `3.14`.
- Width Example: `setw(10)` sets the width of the field to 10 characters. Since the
integer `123` only takes up 3 characters, 7 spaces are added before it.
- Padding with Fill Character Example: setfill('*') << setw(10) << 1234 sets the
width of the output field to 10 characters and uses the asterisk (*) as the padding
character.
BUILT-IN CLASSES FOR I/O
Definition
• Built-in Classes for I/O handle input and output-operations.
• These classes simplify file and console I/O.
Features
• Stream-Based I/O: I/O-classes use streams to manage data flow.
This provides a consistent way to handle data from different sources like the
console or files.
• Overloaded Operators: I/O-classes support overloaded operators.
The `<<` operator is used for output, and the `>>` operator is used for input.
This makes it easy to format and handle data.
• Common built-in classes are listed in below table:
1) `iostream`
2) `istream`
3) `ostream`
1) `iostream`
• Description: The Base-class for input and output-stream classes.
• It provides functionalities for both input and output-operations.
• Derived-classes:
`istream`: For input-operations.
`ostream`: For output-operations.
2) `istream`
• Description: A class for input-stream operations.
• It is used for reading data from input-sources like the keyboard or files.
• Common Functions:
`>>` (extraction-operator) for reading formatted data.
`getline()` for reading lines of text.
`eof()` to check if the end of the input-stream is reached.
3) `ostream`
• Description: A class for output-stream operations.
• It is used for writing data to output-destinations like the console or files.
• Common Functions:
`<<` (insertion-operator) for writing formatted data.
`flush()` to flush the output buffer.
`endl` for inserting a newline and flushing the stream.
Example Program: Demonstrating istream and ostream
#include <iostream>
int main() {
int number;
cout << "Enter an integer: "; // Using ostream to print to console
cin >> number; // Using istream to read from keyboard
cout << "You entered: " << number << endl;
return 0;
}
Output:
Enter an integer: 42
You entered: 42