1

Given the input: const char input[] = "lorem\t, ipsum" and the expected outputs: char first[size(input)] and second[size(input)] I can do this:

sscanf(input, "%s , %s", first, second);

Ignoring white-space and the delimiting character with ,. I'd like to do the same thing with a stream, but the best I can come up with is:

istringstream foo(input);

foo >> first;
foo.ignore(numeric_limits<streamsize>::max(), ',');
foo >> second;

What I'm really looking for is something more like:

istringstream(input) >> first >> ',' >> second;

But obviously that's illegal. I've thought of:

  1. A dummy value to extract the delimiter into
  2. A regex iterator

I don't know if there's a more elegant solution, I'd certainly like something that I could do inline, similar to the sscanf solution.

Live Example

kaya3
  • 41,043
  • 4
  • 50
  • 79
Jonathan Mee
  • 36,513
  • 20
  • 115
  • 270
  • 1
    A dummy value seems reasonable to me. – john Jun 20 '19 at 14:33
  • 2
    `char eater; ...; stream >> foo >> eater >> bar` is a common occurrence in my parsing code for just this purpose. – NathanOliver Jun 20 '19 at 14:35
  • Thanks to @NathanOliver I now have a large monster with big, googly eyes and fluffy blue fur that gobbles up characters stuck in my mind. I think I will call him `char`rie Monster and pray I'm not sued by the Children's Television Workshop. – user4581301 Jun 20 '19 at 15:31
  • 3
    FWIW, you could use something like [this](https://stackoverflow.com/a/23305012/4342498) to make the code more expressive and get some error handling. – NathanOliver Jun 20 '19 at 15:51
  • 1
    Per NathanOliver: possible duplicate of [C++ alternative for parsing input with sscanf](https://stackoverflow.com/questions/23304177/c-alternative-for-parsing-input-with-sscanf) – Davis Herring Jun 20 '19 at 23:55

0 Answers0