last day (15 days later) » 

16:46
0
Q: Writing raw bytes to iostream

mewaI was working on my custom buffer and stream recently and got stuck at the fact that I can't call my stream's behaviour from a pointer to iostream. template <int N> std::iostream& myStream::operator<<(char (&rhs) [N]) { write(rhs, N); return *this; } myStream* pS = new myStream(); std::...

I'm afraid I can't use boost here. As for the rest, as I've already mentioned I created my custom buffer as well, but the standard iostream operators provide wrong format i.e. I need a different one
It needs to be raw binary format
Then you shouldn't overload the inserter for this. The std::streambuf will be fine. Can you give us an mcve?
@0x499602D2 added
It doesn't look like that operator<<() overload is needed at all.
@0x499602D2 It doesn't work without overloading it though
16:46
@mewa What do you mean by it doesn't work? By your question is sounds like operator<<() wasn't being called in the first place. Also, that is not an mcve. Give us a minmal program that we can run ourselves to reproduce the same behavior.
@0x499602D2 data is written to wrong position. I get the impression, that when using char arrays the std iostream operator<< looks for a terminating \0, and also doesn't write that 0, which, since I'm writing a byte stream is a valid value as well, so I can't discard it.
Are you sure it's calling underflow()? That member is called by input stream functions. write() calls overflow().
@0x499602D2 yes, sorry, I listed the wrong function by mistake. I updated my post accordingly.
Thanks. I am reading the question
What different behavior are you seeing when overloading operator<< and when not?
17:01
Provided you with a debug view. It's using exactly the code I provided you with, so no operator overloading, just the standard one.
As you can see the `ABCD` string in both the first and second occurence is followed by the very same characters, which is why I think the std operator is reading from char arrays until it meets a `\0`
Does your char array not have a terminating '/0'?
And when I'm using the overloaded operator, it is writing just the raw data contained within the N characters from the template. However, when I'll be referring to this object from a iostream* pointer it won't call the templated derived class version
Yes, it doesn't; it's pure binary data. It could be just a char array of any size that would serve as a temporary buffer when interfacing from C.Y
And since it's pure binary data, I can't just read 0-terminated strings, because I won't know when it's a terminating 0, and when it's a 0 that is part of the data
Then just don't overload operator<<. Call write directly from bb: bb.write(buf, sizeof buf);.
17:17
I have to though.
It's meant to be used within a function that returns mainly an `fstream` as an `iostream&` reference. But sometimes it needs to return data that isn't accessible as a file and that's why I'm writing my custom `iostream` - so that from outside it can be used just as any `iostream`
Overloading operator<< still isn't the way to go since it will call the base class version when invoked from a reference or pointer of type std::iostream. Anyway, everything should be done at the stream buffer level. The user should have to add an terminating byte at the end of the character array.
How will I know when it's the end of the output and when it's still the data? 0 is a valid value as well
17:32
Maybe have the user append a control sequence like, for example, header data on a packet or the byte order mark on a utf8 document.
Either that or do away with the stream classes and go with passing around stream buffers.
That sounds like a loooong way around :/
But I guess without operators being virtual it's no use trying to do it some other way using std streams..

last day (15 days later) »