Modify a stringstream in-place

Hi,

I have a stringstream that contains encoded (hex) text in the following order (breaks are for clarity only):

First 8 characters: 00000007 (representing a number of columns)
Second 8 characters: 0000005d (representing an integer number of rows)
Remaining characters: 0a5b....6c2d (this can be any number of characters, it was encoded from a set of data)

In other words, the stream contains 000000070000005d0a5b....6c2d

I want to pass the stream contents into a library function "decode(&istream)" that will decode it. Assume for this example there are 4000 characters of actual data (i.e. ignoring the first 16). The decode function expects a stream that specifies the length of the data stream, then 'x' as a separator, and finally the data, i.e. it must be in the following format:

4000x0a5b....6c2d

Could someone please tell me how I can modify the stream and then pass it to the decode function? The stream could be large, potentially megabytes of data, so duplicating it could cause the process to run out of memory. The original number of columns and rows can be discarded, they are no longer relevant. I would guess this could be easily done with pointers, but I have not been able to figure out how.

Thanks for any help you can offer.
Does this help?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <sstream>

using namespace std;

int main( int argc, char * args[] )
{
    stringstream ss( "asdf123" );
    cout << ss.str() << endl;
    
    string s( ss.str() );
    int count( 3 );
    stringstream head;
    head << count << "x";
    s.replace( s.begin(), s.begin() + 4, head.str() );
    ss.str( s );

    cout << ss.str() << endl;

    return 0;
}


Output:

asdf123
3x123


I don't think that you can really edit the data inside a stream; but you can pull it out edit it and put it back.
Last edited on
That works great, thanks a lot for the speedy reply! :-)
I think seymore's answer is not suitable, cause 'string s( ss.str() );
' involves duplicating large string.
Topic archived. No new replies allowed.