2

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?

AusCBloke
  • 18,014
  • 6
  • 40
  • 44
user975900
  • 77
  • 4
  • 11

1 Answers1

6
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;
K-ballo
  • 80,396
  • 20
  • 159
  • 169