This:
#include <iostream>
#include <sstream>
#include <inttypes.h>
using namespace std;
int main (void) {
istringstream iss("123 42");
int8_t x;
while (iss >> x) {
cout << x << endl;
}
return 0;
}
Produces:
1
2
3
4
2
But I want:
123
42
Casting iss >> (int)x
(I initially tried this with a char
) gives me "error: invalid operands to binary expression ('istringstream' (aka 'basic_istringstream') and 'int')" (clang) or "error: ambiguous overload for ‘operator>>’" (g++).
Is there a way to read the value as a number directly into an 8-bit type, or do I have to use an intermediary store?
1 2 3 4 2 2
is still wrong, as2
is printed twice in the end. And it is because of the wrong loop. You should use loop likewhile(stream >> item){ ... }
. – Nawaz