C++

C++ istream Functions

The term “stream” in C++ describes the flow of characters between the program thread and i/o. C++ stream classes are needed to input and output actions on IO devices and files. These classes are designed to manage the program’s input and output and they have particular functionality.

Stream Class Hierarchy

A C++ class is made up of methods required to manage and govern the data it contains.

float, doubles, and classes are data types similar to int. A unique variable with a class as its data type is referred to as a C++ object. The pre-specified special objects ‘cin’ and ‘cout’ have various classes as their data types.

The data written to ‘cout’ or read from ’cin’ are examples of data flows into or out of programs in C++. Currently, we are considering the following four classes for this one:

Istream
Any purpose may be served by this input stream. An illustration of an istream is cin.

Ostream
It is an output stream with several uses. Ostreams come in the forms of cout and cin.

If stream
It is a stream of input files.

Of stream
It is a stream of output files. Inheritance is a notion that is heavily used in object-oriented programming, such as in C++, where some classes take on the characteristics of other classes that have already been created. The ancestor classes then become specializations of themselves through the inclusion of new features.

If stream class
An ifstream can be treated in the same manner as an istream, which is what it is.

Of stream class
In the same way that ifstreams functions, but with output instead of input, an ofstream is an output file stream. An ofstream is used in the same way as cout after being constructed, opened, and verified to have no errors.

Ios class
From this class, all stream classes descended. Input and output streams are two different types.

It defines the stream parts that are independent of the stream’s input or output status in the ios base.

As opposed to the members that are discussed in the ios base, the members that depend on the template parameters are described in this part.

Stream(input)
Due to the iostream library’s complexity, we won’t be able to cover it entirely in these lessons. However, we’ll highlight the functions that are used the most. In this section, we’ll look at the input class from a variety of angles (istream).

We have learned that the extraction operator (>>) will be utilized to acquire the data from an input stream.

ISTREAM

In the C++ programming language, the input stream is handled by the istream class. The input is read and understood as a series of characters using these input stream objects. The input is handled by the cin.

Member Classes

istream::sentry
A class that carries out many tasks and each time an input procedure is performed. Its destructor is not required to carry out any actions. However, implementations may execute additional startup or cleanup tasks on the stream shared by all input operations by using the creation and destruction of sentry objects.

Functions

istream::gcount
Gives the character count that was obtained from the object’s most recent unformatted input action. The unformatted input procedures— get, getline, ignore, peek, read, read some, putback, and unget— alter the value returned by this function. However, keep in mind that calling peeks, putback, or unget does not extract any characters. As a result, the count will always return 0.

istream::get
The single character takes one character out of the stream. The character is either set as the argument’s value or returned (first signature) (second signature).

C string: If the delimiting character is present, it is not removed from the input sequence instead kept as the following character to be retrieved from the stream if it is present.

istream::getline
Removes characters from the stream as unformatted input and saves them as a c-string in the variable “s” until the extracted character becomes the delimiting character or “n” characters have been written to ‘s’. Additionally, the method will stop extracting characters if it reaches the end of the file.

Internally, the function creates an object before accessing the input sequence. Finally, it kills the object before returning, extracting characters from its associated stream buffer object (assuming everything is in order) as if executing one of its member methods, sbumpc or sgetc.

istream::ignore
Characters are taken from the input sequence and discarded one at a time until either “n” characters have been taken out or one compares equal to the delim. Additionally, if the end of the file is reached, the function halts character extraction. The function sets the “eofbit” flag if it reaches this point too soon (before extracting n characters or discovering delim).

Before accessing the input sequence, the function constructs a sentry object internally (with noskipws will be true). Finally, it kills the sentry object before returning, extracting characters from its associated stream buffer object (assuming everything is in order) as if executing one of its member methods: sbumpc or sgetc.

istream::operator>>
The extraction operator (>>) applies this operator to an input stream. It has too many members as a function.

Arithmetic Type

Characters are taken out of the stream and parsed sequentially to represent a value of the right type, which is then saved as the value of “val”. Before accessing the input sequence, the function constructs a sentry object internally (noskipws will be false). If everything checks out well, it then runs num get::get to complete the extraction and parsing processes while modifying the internal state flags of the stream. The sentry object is ultimately destroyed before it leaves.

The function internally generates a sentry object before reading the input sequence, it is viewed as performing formatted input. Finally, it kills the sentry object before returning, extracting characters from its associated stream buffer object (assuming everything is in order) as if executing one of its member methods, sbumpc or sgetc.

Manipulators

The pf (*this) is called, where pf could be a manipulator. Manipulators are routines created particularly to be called when this operator is invoked. The input sequence is unaffected by this procedure and no characters are extracted.

istream::putback
The restored character tries to move the cursor down one character in the stream so that the last character retrieved from the stream can once again be extracted by input operations.

Before accessing the input sequence, the function constructs a sentry object internally. It then calls sputbackc(c) on its associated stream buffer object (if good). The sentry object is ultimately destroyed before it leaves.

istream::read
Read a data block:

The ‘n’ characters are taken from the stream and are then stored in the array that is pointed to by “s”. This function merely replicates a block of data without inspecting the data’s contents or adding a null character at the end.

Within the function, a sentry object is first created before the input sequence is accessed. Finally, it kills the sentry object before returning, extracting characters from its associated stream buffer object (assuming everything is in order) as if executing one of its member methods: sbumpc or sgetc.

Calling member count will return the total number of characters this function has properly read and stored.

istream::readsome
This function is dependent on the inner workings of the specific stream buffer object linked to the stream, whose behavior is largely implementation-defined for standard classes.

Within the function, a sentry object is first created before the input sequence is accessed. Then (if successful), it calls the member function in the stream buffer object’s avail to determine how many characters are now available before calling “sbumpc” to extract up to that number of characters (or sgetc). The sentry object is ultimately destroyed before it leaves.

istream::seekg
The position in the input line determines where the following character will be pulled from the input stream. Prior to reading the input sequence, the function internally constructs a sentry object. Then (if OK), it makes one of two calls to the corresponding stream buffer object: pubseekpos (1) or pubseekoff (2), (If any). Finally, it obliterates the sentry item and departs.

istream::sync
Align the input buffer:

Brings the linked stream buffer’s controlled input sequence into sync. The specific implementation of the stream buffer object connected to the stream determines the operation’s specifics.

istream::tellg
Obtain the input sequence’s position:

It gives the current character’s position in the input stream. The function then returns -1. If the member fails, it returns true.

It returns rdbuf()->pubseekoff if not (0,cur,in). The sentry object is ultimately destroyed before it leaves.

istream::unget
Remove character:

Tries to move the cursor down one character in the stream so that the last character retrieved from the stream can once again be extracted by input operations.

Non-member

operator>> (istream)
The extraction operator (>>) provides formatted input with this action when applied to an input stream.

One character: The character sequence takes the next character out of it and stores it as the value of “c”. The process of extracting characters from is and storing them in s, as a c-string, stops (if the width is not zero) when a whitespace character is encountered or (width ()-1) characters have been extracted.

Extraction of rvalue: enables extracting from rvalue istream objects, which has the same result as extracting from lvalues: It calls is>>Val.

Example 1

In this case, we’d examine how we may obtain any value from the user and then display it as the result on the screen.

#include <iostream>
using namespace std;
int main()
{
    int no;
    cout<<"Please enter a number ";
    cin>>no;
    cout<<"The entered value is: " << no << "\n";
}

We would include the <iostream> header file. Next, we’ll use the namespace ‘std’ in the standard namespace. We would call the function main(). A variable named “no” will be declared within this method. The “int” data type is represented for this variable. The term “int” is the abbreviation of an integer. Afterward, the ‘cout’ statement is used.  This command prints the line.

The user will input any random value after this text has been displayed on the screen. Then, the “cin” statement would have been utilized. This command requires the user to indicate the value. The user’s entered number would be saved in a variable. The value provided by the user would be displayed on the screen using the ‘cout’ command.

Example 2

In this instance, we would see how we get any name from the user and then show it on the screen as the outcome.

#include<iostream>
using namespace std;

int main()

{

    char name[125];
    cout << "Enter your name" << endl;


    cin >> ws;

    cin.getline(name,125);

    cout << name << endl;

    return 0;

}

We would incorporate the header file <iostream>. Next, we will utilize the standard namespace as ‘std’. We would invoke the main() function. Within this method, we will declare a variable. This variable holds the ‘char’ data type. Then the ‘cout’ statement is employed. This command shows the text ‘Enter your name’ on the screen. After displaying this text on the screen, the user would enter any name. Then, we would utilize the ‘cin’ statement.

This command takes the value from the user. The provided name would be stored in a variable ‘ws’. Here, once again, we would utilize the ‘cin’ command. Within this command, the getline() function is being applied. This function contains the parameter which includes the name entered by the user and the length of the name. The ‘cout’ command will be used to show the name provided by the user. To terminate the code, the ‘return 0’ command will be used.

Conclusion

First, we talked about what is C++ istream functions. Then we observe several functions and important definitions mentioned. In this article, we have run different codes that contain a variety of istream functions. In the first code, we take any number from the user and display that number on the screen. In the second one, the user entered the name and printed that name on the screen.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.