user975900
user975900

Reputation: 77

C++ alternative to sscanf()

I have the following function:

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

How can I achieve the same output in a C++ way, rather than using sscanf?

Upvotes: 2

Views: 3453

Answers (1)

K-ballo
K-ballo

Reputation: 81349

int d = maxdepth;
sscanf(s, "%*s%d", &d);

Reads a string (which does not store anywhere) and then reads a decimal integer. Using streams it would be:

std::string dont_care;
int d = maxdepth;

std::istringstream stream( s );
stream >> dont_care >> d;

Upvotes: 6

Related Questions