(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?
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)?
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
}
}
}