0

I am trying to read the contents of an ostrstream using the str (). While trying to do so, i always come across access violations and my application crashes. Is there a way to read from strstream without causing stream errors?

I am working on a legacy project built on Borland C++. I am presently using Borland C++ v5.02 for building my project. Since the code is vast and scattered over a large number of files, I am unable to paste the code here. However, I will try to highlight my use case.

ps is the stream which is being used throughout the project to print receipts. I need to get the receipt data from this strstream without breaking the code.

string str = ps.pStr->str ();

ps.Pstr->rdbuf ()->freeze (0);

ps << EndJob;

The last line causes access violation

rioCoder
  • 11
  • 1
  • 5
  • Example solution: your_stream << std::ends; your_stream.str(); // using result here your_stream.freeze(false); – Defter Oct 07 '16 at 09:44
  • Thank you Defter. I had an additional question though. What i actually need to do is read from the strstream, get the data and leave the stream in a state where it can be used again. I tried the rdbuf ()->freeze (0) command but after extracting the data the stream cannot be used. – rioCoder Oct 07 '16 at 09:48
  • Please add sample code in your question maybe there some more probem – Defter Oct 07 '16 at 09:56

1 Answers1

0

You missed set null in the end of the buffer.

Before any call to str() that uses the result as a C string, the buffer must be null-terminated, typically with std::ends.
Defter
  • 214
  • 1
  • 7
  • Thanks a lot Defter. I was not able to use std::ends so i just inserted a null character '/0'. This seems to have fixed the issue. :) – rioCoder Oct 07 '16 at 10:57