Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ cin vs. C sscanf

So i wrote this in C, so sscanf scans in s but then discards it, then scans in d and stores it. So if the input is "Hello 007", Hello is scanned but discarded and 007 is stored in d.

static void cmd_test(const char *s)
{
    int d = maxdepth;
    sscanf(s, "%*s%d", &d);
}

So, my question is how can I do the same thing but in C++? possibly using stringstream?

like image 978
Patrick Johnston Avatar asked Dec 20 '25 05:12

Patrick Johnston


1 Answers

#include <string>
#include <sstream>

static void cmd_test(const char *s)
{
    std::istringstream iss(s);
    std::string dummy;
    int d = maxdepth;
    iss >> dummy >> d;
}
like image 187
ildjarn Avatar answered Dec 22 '25 20:12

ildjarn