1

I'm going to try asking this again, but better than the last time.

I have a program that reads binary data in from various places, and then manipulates it in a class that I have written. The data will be read originally from it's source (which may vary), then get written to a stream, and that stream will be passed into the class for the class to work on.

My struggles are with figuring out how to create an iostream and write to/read from it. I have looked in various places and read the references at cplusplus.com, but I can't find a simple example of how to create an iostream.

based on what I've read, here is what I tried:

#include <iostream>


using namespace std;

int main(){
    streambuf* sb;
    iostream s(sb);

    s.put('h'); //segfault
}

Frankly, I have no idea why its segfaulting, or how to fix it, so I am asking for someone to tell me how to properly create an iostream object that, ultimately, I will be able to execute something like the following on:

void printByN(iostream s, n){
    while (s.peek() != eof()){ // I'm not sure this is correct. need help with that too
        char buf [n];
        s.read(&buf, n);
        cout << buf << endl;
    }
}

int main(){
    //create iostream s;
    char* buf = "hello there my friend";
    s.write(buf, strlen(buf));
}

The point is I need the stream to know when it is empty, and return a special value at that point. I cannot use a stringstream, because it is possible for the binary data to contain null characters that ARE NOT meant to terminate the data.

If iostream is the wrong way to do this, please let me know of a better option.

ewok
  • 20,148
  • 51
  • 149
  • 254
  • 4
    I would recommend getting a [decent introductory C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), rather than trying to learn the language from scratch from random websites. You should have much greater success! – Oliver Charlesworth Jan 03 '13 at 16:12
  • You're right, that was an assumption, so apologies. But your code above seems to have basic errors, like passing an uninitialised pointer as an argument to the constructor... – Oliver Charlesworth Jan 03 '13 at 16:17

1 Answers1

6

stringstream is exactly what you want. C++ strings can contain embedded null characters (\0). If you don't believe me, try it.

iostream is really an abstract base class and has no real functionality on its own. You should not be creating instances of iostream directly.

cdhowie
  • 158,093
  • 24
  • 286
  • 300
  • You're right. The problem with the null character was not that the `stringstream` thought it was at its end, it was that the test of `while (s.peek())` was not working properly. can you recommend the correct way to perform this test? – ewok Jan 03 '13 at 16:26
  • @ewok I would suggest `while (s.peek() != EOF)`. You can also test `s.eof()` or `s.bad()` etc. after the peek operation for more specific information. – cdhowie Jan 03 '13 at 16:29
  • it's telling me that `EOF` was not declared. and using `s.eof()` never reaches the end. – ewok Jan 03 '13 at 16:31
  • @ewok You would have to `#include ` I think. – cdhowie Jan 03 '13 at 16:32
  • that works, except I was inputting `"hello the\0re my\0\0\0 friend"` and it terminates after `"hello the"`, on the NULL character, the same way it was before. I think the real question is probable, "Why is the eof flag not getting set?" – ewok Jan 03 '13 at 16:34
  • 1
    @ewok Yes, you need to explicitly specify the length of the input string if it contains embedded nulls. When given a `char const *` without a length, C++ string classes will find the length using the null-termination test. If you specify the length yourself, this test is skipped. – cdhowie Jan 03 '13 at 16:42