Optional parameter in stringstream.

Hey guys,

I'm reading in a data set using an ifstream, then fetching line by line to a stringstream:
1
2
3
4
	std::ifstream sfile(filename);    
	std::string test;                 
	std::getline(sfile, test);
	std::stringstream sline(test);

(I'm not sure why I used an intermediate string; it's pretty much legacy-code at this point, which I just reuse every time. Still works, so why change it!)

The problem is I'm using two types of data sets now, and the difference is one (optional). Most data files just have an arbitrarily large number if the second must be ignored, but others have nothing.

In the normal case, I'd simply use sline >> d >> L; to extract the parameter values. However, I'm not sure how this line will behave if the second parameter is omitted. Will it read nonsense? How do I check whether or not the parameter was set or not?
what do you mean with 'intermediate string'?

Will it read nonsense?
No, it will raise an eof

How do I check whether or not the parameter was set or not?
check eof or whatever signals that L wasn't read (the initial value since it isn't change)
Last edited on
what do you mean with 'intermediate string'?


For some reason, I'm reading each new line into a string and then converting that string to a stringstream so I can extract parameters. That seems a bit redundant. Is it possible to read a line directly into a stringstream?

So, I can just "ignore" it and check whether L has a non-zero value (given that I initialize it as 0 and would never have 0 as value if it was set)?
Something like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
std::ifstream sfile(filename);    
std::string test;                 
if( std::getline(sfile, test) ) // if a line was read
{
    std::istringstream sline(test);

    double d ;
    if( sline >> d ) // if a double was read
    {
         // do something with d
    
         long l ;
         if( sline >> l ) // if a long was read
         {
              // do something with it
         }    
     }
}
Does that work? According to the reference, the >> operator returns the stream object itself, which doesn't sound very helpful in this case.
1
2
3
4
if( sline >> d ) // if a double was read
{
    // ...
}


is equivalent to:

1
2
3
4
5
sline >> d ; // try to read a double
if( !sline.fail() ) // if the stream is not in a failed state
{
    // ...
}


See: http://en.cppreference.com/w/cpp/io/basic_ios/operator_bool
Is it possible to read a line directly into a stringstream?
Nope. The stream doesn't offer such an option

So, I can just "ignore" it and check whether L has a non-zero value (given that I initialize it as 0 and would never have 0 as value if it was set)?
It is possible. You need to make sure that it is not set from a previous operation. What JLBorges shows is safer.

Does that work? According to the reference, the >> operator returns the stream object itself, which doesn't sound very helpful in this case.
it works because the stream has an operator that leads to bool (otherwise it wouldn't compile).

See
http://www.cplusplus.com/reference/ios/basic_ios/operator_bool/
Never heard of operator bool. Thanks for the info, both of you!
Topic archived. No new replies allowed.