Use istrstream to read int, float and char : istrstream « File Stream « C++ Tutorial






#include <iostream>
#include <strstream>
using namespace std;

int main()
{
  char s[] = "10 Hello 0x75 42.73 OK";

  istrstream ins(s);

  int i;
  char str[80];
  float f;

  // reading: 10 Hello
  ins >> i;
  ins >> str;
  cout << i << " " << str << endl;

  // reading 0x75 42.73 OK
  ins >> hex >> i;
  ins >> f;
  ins >> str;

  cout << hex << i << " " << f << " " << str;

  return 0;
}
10 Hello
75 42.73 OK"








12.17.istrstream
12.17.1.Attempt to read from empty stream
12.17.2.Demonstrating input from an istringstream object
12.17.3.Use istrstream to read int, float and char
12.17.4.How to read the contents of any array that contains text
12.17.5.Read and display binary data