Data saved in my file is (white spaces added at both beginning and end on purpose for this test):
1 2 3
Loading the data using the code below with or without "std::ws" does not cause any difference. So I am confused by the role of "std::ws" as I have seen code using it. Can someone explain a little bit? Thanks!
void main ()
{
ifstream inf;
inf.open ("test.txt");
double x=0, y=0, z=0;
string line;
getline(inf, line);
istringstream iss(line);
//Using "std::ws" here does NOT cause any difference
if (!(iss >> std::ws >> x >> y >> z >> std::ws))
{
cout << "Format error in the line" << endl;
}
else
{
cout << x << y << z << endl;
}
iss.str(std::string ());
iss.clear();
cin.get();
}
std::ios_base::noskipws
on or are using unformatted input thenstd::ws
can be useful. – Liebermannskipws
;noskipws
is a manipulator function. – Armitagestd::ios_base::skipws
is disabled but rather with unformatted input which doesn't skip leading whitespace. – Hartsell