0

我正在尝试使用 str() 读取 ostrstream 的内容。在尝试这样做时,我总是遇到访问冲突并且我的应用程序崩溃。有没有办法从 strstream 读取而不会导致流错误?

我正在开发一个基于 Borland C++ 的遗留项目。我目前正在使用 Borland C++ v5.02 来构建我的项目。由于代码庞大且分散在大量文件中,因此我无法在此处粘贴代码。但是,我将尝试强调我的用例。

ps 是整个项目中用于打印收据的流。我需要在不破坏代码的情况下从此 strstream 获取收据数据。

字符串 str = ps.pStr->str ();

ps.Pstr->rdbuf()->冻结(0);

ps <<结束作业;

最后一行导致访问冲突

4

1 回答 1

0

您错过了缓冲区末尾的 set null 。

Before any call to str() that uses the result as a C string, the buffer must be null-terminated, typically with std::ends.
于 2016-10-07T09:40:15.647 回答