Reputation: 6467
As far as I know std::cin
skips all white spaces, is this the case with all the other std::istream
: std::fstream
, std::sstream
, std::iostream
objects, do they skip white space? For example, if you want to read from file containing white space separated values, do you need to specify skipping in the same order you specify any other input format structure?
Example, if you read values in the format (val1, val2):
char par1, comma, par2;
double x,y;
is >> par1 >> x >> comma >> y >> par2;
// check if any input
if(!is) return is;
// check for valid input format
if (par1 != '(' || comma != ',' || par2 != ')')
For white space separated values, do you need to specify white space as format marker?
int val1;
char whSp= ' '; // or string whSp = " ";
is >> val1 >> whSp;
Upvotes: 2
Views: 1268
Reputation: 21000
By default yes, although this behavior is governed by the base class std::ios_base
. When the stream initializes its buffer by calling init
, it will also set the skipws
flag (among other things); this flag is used by formatted input functions (such as operator>>
) to determine whether to skip whitespace or not. Unless this flag is later modified directly or through a manipulator, formatted functions will always skip whitespace.
As to which characters are considered whitespace this depends on the ctype
facet of the locale that is imbued in the stream at the time the function is called, for the default C locale (and indeed most locales) these are \t, \n, \v, \f, \r and space.
Upvotes: 6
Reputation: 27365
As far as I know cin skips all white spaces, is this the case with all the rest: fstream, sstream, iostream, do they skip white space?
The behavior of all standard stream types (with regards to white space), is the same (otherwise they would break the Liskov Substitution principle).
For example, if you want to read from file containing white space separated values, do you need to specify skipping in the same order you specify any other input format structure?
That's a correct way to do it. You can also use peek and ignore to skip characters.
For white space separated values, do you need to specify white space as format marker?
The example you give here is not correct.
Either way, to control the space controlling policy, have a look at std::skipws and std::noskipws stream manipulators.
Upvotes: 2