c++parsingscanf

C++ alternative for parsing input with sscanf


Assuming my program expects arguments of the form [ 0.562 , 1.4e-2 ] (i.e. pairs of floats), how should I parse this input in C++ without regular expressions? I know there are many corner cases to consider when it comes to user input, but let's assume the given input closely matches the above format (apart from further whitespace).

In C, I could do something like sscanf(string, "[%g , %g]", &f1, &f2); to extract the two floating point values, which is very compact.

In C++, this is what I've come up with so far:

std::string s = "[ 0.562 , 1.4e-2 ]"; // example input

float f1 = 0.0f, f2 = 0.0f;

size_t leftBound = s.find('[', 0) + 1;
size_t count = s.find(']', leftBound) - leftBound;

std::istringstream ss(s.substr(leftBound, count));
string garbage;

ss >> f1 >> garbage >> f2;

if(!ss)
  std::cout << "Error while parsing" << std::endl;

How could I improve this code? In particular, I'm concerned with the garbage string, but I don't know how else to skip the , between the two values.


Solution

  • The obvious approach is to create a simple manipulator and use that. For example, a manipulator using a statically provided char to determine if the next non-whitespace character is that character and, if so, extracts it could look like this:

    #include <iostream>
    #include <sstream>
    
    template <char C>
    std::istream& expect(std::istream& in)
    {
        if ((in >> std::ws).peek() == C) {
            in.ignore();
        }
        else {
            in.setstate(std::ios_base::failbit);
        }
        return in;
    }
    

    You can then use the thus build manipulator to extract characters:

    int main(int ac, char *av[])
    {
        std::string s(ac == 1? "[ 0.562 , 1.4e-2 ]": av[1]);
        float f1 = 0.0f, f2 = 0.0f;
    
        std::istringstream in(s);
        if (in >> expect<'['> >> f1 >> expect<','> >> f2 >> expect<']'>) {
            std::cout << "read f1=" << f1 << " f2=" << f2 << '\n';
        }
        else {
            std::cout << "ERROR: failed to read '" << s << "'\n";
        }
    }