1

I have an example string:

test1\0test2\0test3\0

And I want to be able to copy it to a stringstream. I've tried the following methods which are not working:

sStream << teststring;
sStream.write(teststring, 99);

Is there a simple way to copy text to a stringstream while ignoring null characters?

Smurph269
  • 65
  • 1
  • 6
  • What is the output you get with that example? – David G Oct 24 '13 at 19:46
  • The output I am getting is "test1". Everything beyond the first null is left out. – Smurph269 Oct 24 '13 at 19:49
  • [Works fine for me](http://ideone.com/t0EOQU). – Kerrek SB Oct 24 '13 at 19:51
  • I'm actually reading the string from a socket. I get the correct number of bytes read, but when I try to add the buffer to the stringstream I only get the first section. – Smurph269 Oct 24 '13 at 19:56
  • I am trying to write the string to the stringstream. It starts as a char array in a socket buffer, so std::string is not being used. Here is an example of what I am seeing: [example](http://ideone.com/Lb35vf) – Smurph269 Oct 24 '13 at 20:19

1 Answers1

3

Are you using std::string like Kerrek SB? It becomes as simple as the following:

int main()
{
    std::ostringstream ss;
    std::string testString("a\0b\0c\0d", 7);

    ss.write(&testString[0], testString.size());

    std::cout << ss.str(); // abcd
}
Community
  • 1
  • 1
David G
  • 94,763
  • 41
  • 167
  • 253
  • This pretty much did it. I had tried using std::string before, but I was missing the difference made by using the constructor with two arguments. [This question](http://stackoverflow.com/questions/164168/how-do-you-construct-a-stdstring-with-an-embedded-null) helped out too. – Smurph269 Oct 24 '13 at 20:54