next up previous contents
Next: D.3 vector Up: D.2 簡単な入門 Previous: D.2.9 sprintf() の代りに

D.2.10 sscanf() の代りに

test-sscanf.C

// 古い方法を使うと

#include <iostream.h>
#include <string>
#include <cstdio>

int main()
{
  double x;
  int n;
  string s;
  s = "123.4 56";
  sscanf(s.c_str(), "%lf%d", &x, &n);
  cout << "x=" << x << ", n=" << n << endl;
  return 0;
}

test-strstream3.C

#include <iostream.h>
#include <string>
#include <strstream.h>

int main()
{
  double x;
  int n;
  string s;
  s = "123.4 56";
  istrstream is(s.c_str());
  is >> x >> n;
  cout << "x=" << x << ", n=" << n << endl;
  return 0;
}


next up previous contents
Next: D.3 vector Up: D.2 簡単な入門 Previous: D.2.9 sprintf() の代りに
Masashi Katsurada
平成18年4月28日