1

Maybe can exist a better solution, but this is what I have:

mybuffer   = new char[265536](); // all to zero 
the_stream = new stringstream ;
the_stream->rdbuf()->pubsetbuf(mybuffer,265536);

I have a put function:

template <class TTput_stream>
void W_inpout::W_stream::put (TTput_stream &value) {   
   int a =  sizeof value;
   const char * buffer ;
   buffer = reinterpret_cast<const char*> (&value);
   the_stream->write(buffer,sizeof value);
   loc+= sizeof value;
   if (lof<loc)  lof=loc; 
}

And Get function:

template <class TTget_stream>
void W_inpout::W_stream::get(TTget_stream & value) {
    char * buffer ;
    buffer = reinterpret_cast< char*> (&value);
    the_stream->read(buffer, sizeof value);  
    loc=int(the_stream->tellg());
    the_stream->seekp(the_stream->tellg());
}

It works for me. If I put some values (in example one int, 2 floats, 3 doubles), I can recover them without problem (Doing a seekg before calling the get function).

But I have a problem. I want to access to 'mybuffer' data and I discover that after some 'put' commands I keep having zeros in all the 265536 positions.

for (int x=0;x<265536;x++) { if (mybuffer[x]!=0) .... )

I dont understand what is happening. Where is my data? Why does my buffer seems to be empty?

Mat
  • 202,337
  • 40
  • 393
  • 406
tonnot
  • 435
  • 1
  • 5
  • 17
  • Can you provide code, where you put something, and then you see only zeroes? – fghj Nov 16 '11 at 14:03
  • the code is the showed at the post. I just call : float x = 15: the_stream->put(15); the_stream->seekg(0); float y; the_stream->get(y) – tonnot Nov 16 '11 at 14:05
  • the code is the showed at the post. I just call : float x = 15: the_stream->put(15); the_stream->seekg(0); float y; the_stream->get(y) : This works and if I check mybuffer all is zero. (excuse me for the answer, I dont konw what is happen but I cant press enter, tab , etc ) – tonnot Nov 16 '11 at 14:17

1 Answers1

0

I don't see zeros. Checked with gcc 4.5. If you use Visual Studio, you can find this link usefull:

Setting the internal buffer used by a standard stream (pubsetbuf)

Community
  • 1
  • 1
fghj
  • 8,898
  • 4
  • 28
  • 56
  • Yes, with mingw I dont see zeros. This is a VC problem. What I dont understand is why I have not warning emitted by the compiler ... (I use qt+qtcreator+ms compiler). And I dont see eany decent document showing how to use a class extending public std::streambuf.... – tonnot Nov 16 '11 at 18:51