bada C++ std::字符串流
bada 在读取 stringstream 时崩溃。
json::Object objDocument = d();
std::stringstream stream;
json::Writer::Write(objDocument, stream);
json::Object objDocument2;
json::Reader::Read(objDocument2, stream); // <=== crash
或者像这样:
std::string *requestString = new std::string(data);
AppLog(requestString->c_str()); // <=== contains correct data
std::stringstream stream;
stream << *requestString;
const char *ddd = stream.str().c_str();
AppLog(ddd); // <==== contains random data
我该如何解决它?
谁有想法或有相同的经历?
bada crashed on stringstream read.
json::Object objDocument = d();
std::stringstream stream;
json::Writer::Write(objDocument, stream);
json::Object objDocument2;
json::Reader::Read(objDocument2, stream); // <=== crash
or like this:
std::string *requestString = new std::string(data);
AppLog(requestString->c_str()); // <=== contains correct data
std::stringstream stream;
stream << *requestString;
const char *ddd = stream.str().c_str();
AppLog(ddd); // <==== contains random data
How can I solve it?
Who had ideas or same experience?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
字符串
stream.str()
是一个临时字符串,在使用它获取c_str()
之后指针将不再有效,该字符串会立即被销毁。如果将引用保存在临时文件中,该字符串将保留下来:
The string
stream.str()
is a temporary which is destroyed right after you use it to getc_str()
after which the pointer is no longer valid.If you save a reference in a temporary the string will stick around:
第一个问题可能是寻道问题。写入后,
stringstream
中的当前位置位于末尾,但您想从头开始读取。The first problem is probably a seek issue. After the write, the current position in the
stringstream
is at the end, but you want to read from the start.