C++ manipulator noskipws function is used to clear the skipws format flag for the str stream.
When we set noskipws format flag, then it does not skip any number of whitespaces before the next input. Tab spaces, blank spaces and carriage returns are considered whitespaces. It will consider any whitespace as part of the next input field.
This applies to any formatted input operation performed with operator >> on the stream.
Syntax
ios_base& noskipws (ios_base& str);
Parameter
str : stream object whose format flag is affected.
Return value
It returns argument str.
Data Races
Data races may cause when modifies str concurrent access to the same stream object.
Exceptions
str is in a valid state, if an exception is thrown.
Example 1
Let's see the simple example to demonstrate the use of noskipws:
#include <iostream> // std::cout, std::skipws, std::noskipws
#include <sstream> // std::istringstream
using namespace std;
int main () {
char a, b, c;
istringstream iss (" 123");
iss >> skipws >> a >> b >> c;
cout << a << b << c << '\n';
iss.seekg(0);
iss >> noskipws >> a >> b >> c;
cout << a << b << c << '\n';
return 0;
}
Output:
123
1
Example 2
Let's see another simple example:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
char first, middle, last;
istringstream("G B S") >> first >> middle >> last;
cout << "Default behavior: First Name = " << first << ", Middle Name = " << middle << ", Last Name = " << last << '\n';
istringstream("G B S") >> noskipws >> first >> middle >> last;
cout << "noskipws behavior: First Name = " << first << ", Middle Name = " << middle << ", Last Name = " << last << '\n';
return 0;
}
Output:
Default behavior: First Name = G, Middle Name = B, Last Name = S
noskipws behavior: First Name = G, Middle Name = , Last Name = B
Example 3
Let's see another simple example:
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
char c1, c2, c3;
istringstream("a b c") >> c1 >> c2 >> c3;
cout << "Default behavior: c1 = " << c1 << " c2 = " << c2 << " c3 = " << c3 << '\n';
istringstream("a b c") >> noskipws >> c1 >> c2 >> c3;
cout << "noskipws behavior: c1 = " << c1 << " c2 = " << c2 << " c3 = " << c3 << '\n';
return 0;
}
Output:
Default behavior: c1 = a c2 = b c3 = c
noskipws behavior: c1 = a c2 = c3 = b