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?

3
  • 1
    The biggest question is: what are you trying to do?
    – Poriferous
    Jul 21, 2015 at 12:57
  • Why don't you just search through buffer directly? Indexing isn't exactly rocket science.
    – molbdnilo
    Jul 21, 2015 at 13:14
  • You are right; but I wanted to explore the features of istringstream class.
    – GSi
    Jul 21, 2015 at 15:38

1 Answer 1

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.

1
  • 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, 2015 at 15:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.