2016-10-07 21 views
0

我正在嘗試使用str()讀取ostrstream的內容。在嘗試這樣做時,我總是遇到訪問違規和我的應用程序崩潰。有沒有辦法從strstream中讀取而不會導致流錯誤?試圖從strstream讀取內容導致訪問衝突

我正在構建Borland C++的遺留項目。我目前正在使用Borland C++ v5.02來構建我的項目。由於代碼廣泛且分散在大量文件中,因此我無法在此處粘貼代碼。不過,我會試着突出我的用例。

ps是在整個項目中用來打印收據的流。我需要從這個strstream獲取收據數據而不破壞代碼。

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

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

ps < < EndJob;

最後一行會導致訪問衝突

+0

示例解決方案: your_stream << std :: ends; your_stream.str(); //使用此處的結果 your_stream.freeze(false); – Defter

+0

謝謝Defter。但我還有一個額外的問題。我真正需要做的是從strstream中讀取數據,並將數據流留在可以再次使用的狀態。我嘗試了rdbuf() - > freeze(0)命令,但是在提取數據後,流不能使用。 – rioCoder

+0

請在你的問題中添加示例代碼也許有更多的探針 – Defter

回答

0

你錯過了在緩衝區的末尾設置爲null。

Before any call to str() that uses the result as a C string, the buffer must be null-terminated, typically with std::ends. 
+0

非常感謝Defter。我無法使用std :: ends,因此我只是插入了空字符'/ 0'。這似乎解決了這個問題。 :) – rioCoder