1

why this code

char magicData [] = { 0x00i8, 0xfdi8, 0xffi8, 0xfci8, 0x00i8,
                      0xf3i8, 0xf4i8, 0xf5i8, 0x00i8};
std::string s;
std::istringstream ss(magicData, sizeof(magicData));
while(std::getline(ss, s))
{
std::cout << s << std::endl;
}

don't produce any output? (using stringstream instead of istringstream doesn't help). As result i expect 2 line of string (without 0x00 at end). How to solve it?

sbel
  • 11
  • 3

1 Answers1

1

std::istringstream doesn't have a constructor that takes an array. You're actually calling the constructor that takes a C-style string and an openmode. All you need for this to work is:

std::istringstream ss(std::string(magic, magic + sizeof(magic)));
John Leuenhagen
  • 576
  • 7
  • 23
David G
  • 94,763
  • 41
  • 167
  • 253
  • magicData does not include '\0'. Your answer and OP example will produce UB. – zoska Nov 13 '13 at 12:54
  • but... `char magicData[] = "line1\n line 2\n";` works => istringstream have this such of constructor – sbel Nov 13 '13 at 13:01