C++ ios::Skipws() Function



The C++ std::ios::skipws() function is a manipulator used with the input streams. When it is invoked on the input stream, it makes the stream to skip any whitespace characters (spaces, tabs, newlines) before extracting the actual input. This ensures that whitespace does not interfere with reading data from the stream.

Syntax

Following is the syntax for std::ios::skipws() function.

ios_base& skipws (ios_base& str);

Parameters

  • str − It indicates the stream object whose format flag is affected.

Return Value

This function returns the Argument str.

Exceptions

If an exception is thrown, str is in a valid state.

Data races

It modifies str. Concurrent access to the same stream object may cause data races.

Example

In the following example, we are going to consider the basic usage of the skipws() function.

#include <iostream>
#include <sstream>
int main()
{
    std::istringstream x("  1  12");
    int a, b;
    x >> std::skipws >> a >> b;
    std::cout << "a: " << a << ", b: " << b << std::endl;
    return 0;
}

Output

Output of the above code is as follows −

a: 1, b: 12

Example

Consider the following example, where we are going to disable the skipws() function.

#include <iostream>
#include <sstream>
int main()
{
    std::istringstream a("  1  22");
    int x, y;
    a >> std::noskipws >> x >> y;
    std::cout << "x : " << x << ", y : " << y << std::endl;
    return 0;
}

Output

Following is the output of the above code −

x : 0, y : 32761
ios.htm
Advertisements