Search code examples
c++istream

Easiest way to read a null terminate string with istream?


I have an istream and i need to read exactly a specific amount of bytes BUT i dont know the length of it. It is null terminated. I was thinking i could either 1) Write a loop and read one byte at a time 2) Tell it to return me a buffer or string which starts now until a certain byte (0 in this case). or 3) Read into a buf exactly one byte at a time and check it for 0 and append it to string if it isnt.

The 3rd i know i can do but the other 2 sounds like it may be possible with istream (its a filestream in this case). I am still reading documentation for istream. Theres a lot.


Solution

  • Since you don't know the length of it the simplest would be:

    std::string strBuf;
    std::getline( istream, strBuf, '\0' );