In this lesson, you will learn.
The following figure shows the arrangement of the most important classess stream classes in C++.
The ios class is the granddaddy of all stream classes and contains most of the features needed to operate the C++ stream. The ios class is declared the virtual base class so that only one copy of its member is inherited by the iostream.
The istream and ostream classes are derived from ios and are dedicated to input and output operations, respectively
The istream class contains such functions as get(), getline(), read(), and the overloaded extraction (>>) operators, while ostream contains put() and write(), and the overloaded insertion (<<) operators.
The iostream class is derived from both istream and ostream by multiple inheritances. Classes derived from it can be used with devices, such as disk files, that may be simultaneously opened for both input and output.
Three classes—istream_withassign, ostream_withassign, and iostream_withassign—are inherited from istream, ostream, and iostream, respectively. They add assignment operators to these classes.
Provides an interface to physical devices through buffers and acts as a base class for filebuf.
The istream and ostream classes define the two functions i.e. get() and put() to handle single-character I/O operations.
//StreamFunctionEx1.cpp
#include<iostream>
using namespace std;
int main(){
int count=0;
char c;
cout<<"Enter text\n";
cin.get(c);
while(c!='\n'){
cout.put(c);
count++;
cin.get(c);
}
cout<<"nTotal characters="<<count<<endl;
return 0;
}
Enter text
I like C++ and object oriented programming.
I like C++ and object oriented programming.
Total characters=43
In C++, getline()
and write()
are two functions commonly used for input and output operations.
getline()
FunctionThe getline()
function is used to read a line of text from an input stream (like std::cin
or a file stream) into a std::string
variable. It reads characters until it encounters a newline character ('\n'
) or the specified delimiter.
std::getline(input_stream, str, delimiter);
input_stream
– The input stream from which to read (e.g., std::cin
).str
– The std::string
object where the input will be stored.delimiter
(optional) – The character at which reading stops (defaults to '\n'
).
#include<iostream>
#include<string>
int main() {
std::string name;
std::cout << "Enter your full name: ";
// Use getline to read the entire line including spaces
std::getline(std::cin, name);
std::cout << "Hello, " << name << "!" << std::endl;
return 0;
}
The cin.getline()
function in C++ is used to read a line of input into a character array (char
array). Unlike std::getline()
, which is used with std::string
, cin.getline()
works specifically with C-style strings.
std::cin.getline(buffer, size, delimiter);
buffer
– The character array where the input is stored.size
– The maximum number of characters to read (including the null terminator \0
).delimiter
(optional) – The character at which reading stops (defaults to '\n'
).size - 1
characters.\0
at the end of the buffer
.std::cin
, which stops reading at whitespace, cin.getline()
can handle multi-word input with spaces.
#include<iostream>
int main() {
char name[50]; // Declare a character array to store the input
std::cout << "Enter your full name: ";
// Use cin.getline to read the input, maximum of 50 characters
std::cin.getline(name, 50);
std::cout << "Hello, " << name << "!" << std::endl;
return 0;
}
cin.getline(name, 50);
reads a full line of input (up to 49 characters plus the null terminator)."John Doe"
, the entire string is stored in the name
array.
#include<iostream>
int main() {
char sentence[100];
std::cout << "Enter a sentence (end input with a semicolon ';'): ";
// Use cin.getline to read input until a semicolon is encountered
std::cin.getline(sentence, 100, ';');
std::cout << "You entered: " << sentence << std::endl;
return 0;
}
';'
."This is a test; More text"
, only "This is a test"
will be stored in sentence
because cin.getline()
stops reading at the semicolon.
size
parameter does not exceed the size of the character array to avoid buffer overflow issues.size
, the extra characters remain in the input buffer and can cause problems for subsequent input operations.cin.getline()
automatically adds a null terminator \0
at the end of the buffer
, so it’s important to allocate enough space in the array.
The write()
function is part of the output operations and is generally used with std::cout
. It outputs a specific number of characters from a char
array or std::string
.
output_stream.write(str, n);
#include<iostream>
int main() {
const char *text = "Hello, World!";
// Output only the first 5 characters using write
std::cout.write(text, 5); // Output: Hello
std::cout << std::endl;
// Output the entire text
std::cout.write(text, 13); // Output: Hello, World!
return 0;
}
write()
function writes exactly n
characters from the provided char
array or C-style string.std::cout.write(text, 5);
outputs only "Hello"
because it reads only the first 5 characters from text
.std::cout.write(text, 13);
outputs the entire string "Hello, World!"
.
Nice !!
very beneficial in learning cpp
You must be logged in to submit a review.