0

I saw a lot of questions on the peek method, but mine concerns a topic which would be almost obvious, but nevertheless (I think) interesting.

Suppose you have a binary file to read, and that you choose to bring up it as a whole in the program memory and use an istringstream object to perform the reading.

For instance, if you are searching for the position og a given byte in the stream, accessing the hard disk repeatedly would waste time and resources...

But once you create the istringstream object any eventual NULL byte is treated as an EOF signal.

At least this is what happened to me in the following short code:

                 // obvious omissis
                 std::istringstream is(buffer);
                 // where buffer is declared as char *
                 // and filled up with the contents of
                 // a binary file
                 char sample = 'a';
                 while(!is.eof() && is.peek() != sample)
                   { is.get(); }
                 std::cout << "found " << sample << " at " << is.tellg() << std::endl;

This code doesn't work neither with g++ 4.9 nor with clang 3.5 in the hypothesis that there is a null byte inside buffer before a match with sample can be found, since that null byte sets the eof bit.

So my question is: Is this kind of approach to be avoided at all or there is some way to teach peek that a null byte is not "necessarily" the end of the stream?

GSi
  • 639
  • 3
  • 9

1 Answers1

0

If you look at your std::istringstream constructors, you'll see (2) takes a std::string. That can have embedded NULs, but if you pass buffer and that's a character array or char*, the string constructor you implicitly invoke will use a strlen-style ASCIIZ length determination to work out how much data to load. You should instead specify the buffer size explicitly - something like:

std::string str(buffer, bytes);
std::istringstream is(str);

Then your while(!is.eof() is bodgy... there are hundreds of S.O. Q&A about that issue; one at random - here.

Community
  • 1
  • 1
Tony Delroy
  • 94,554
  • 11
  • 158
  • 229
  • Thanks for your answer; about `!is.eof()` inside the `while` expression, I knew that it is "out of place". I simply left it there because I wanted to be sure that it was peek() the responsible of setting `eof` bit on... – GSi Jul 21 '15 at 15:37