字符串流提取不起作用
我似乎在从字符串流中提取数据时遇到问题。 我的提取开始似乎缺少前两个字符。
我有类似于以下代码的内容:
std::stringstream ss(
std::stringstream::in |
std::stringstream::out
);
bool bValid;
double dValue;
double dTime;
for( (int i = 0; i < 5; i++ )
{
bValid = getValid();
dValue = getValue();
dTime = getTime();
// add data to stream
ss << bValid;
ss << dValue;
ss << dTime;
}
int strsize = ss.str().size();
char* data = new char[strsize];
std::strcpy(data, ss.str().c_str());
// then do stuff with data
// ...... store data in an Xml Node as CDATA
// read data back
std::stringstream ssnew( std::stringstream in | std::stringstream out );
ss.clear();
ss << getCharData(); // returns a char* and puts it in stream.
for( int i = 0; i < 5; i++ )
{
ssnew >> bValid; // do something with bValid
ssnew >> dValue; // do something with dValue
ssnew >> dTime; // do something with dTime
}
我遇到一个问题,当我在从“ssnew”读取数据时使用提取运算符时,它似乎会跳过前两个字符。 例如,在调试器中,它显示字符串流具有“001.111.62.2003...等”。 然而,在第一个“ssnew >> bValid”之后,bValid 变为“true”,dValue 变为“0.111”,dTime 变为“0.62”,表明流中的前两个零被忽略。 为什么它不从流的开头开始?
干杯, 赛斯
I seem to be having a problem with extracting data from a stringstream. The start of my extraction seems to be missing the first two characters.
I have something similar to the following code:
std::stringstream ss(
std::stringstream::in |
std::stringstream::out
);
bool bValid;
double dValue;
double dTime;
for( (int i = 0; i < 5; i++ )
{
bValid = getValid();
dValue = getValue();
dTime = getTime();
// add data to stream
ss << bValid;
ss << dValue;
ss << dTime;
}
int strsize = ss.str().size();
char* data = new char[strsize];
std::strcpy(data, ss.str().c_str());
// then do stuff with data
// ...... store data in an Xml Node as CDATA
// read data back
std::stringstream ssnew( std::stringstream in | std::stringstream out );
ss.clear();
ss << getCharData(); // returns a char* and puts it in stream.
for( int i = 0; i < 5; i++ )
{
ssnew >> bValid; // do something with bValid
ssnew >> dValue; // do something with dValue
ssnew >> dTime; // do something with dTime
}
I am having a problem that when I use the extraction operator when reading data from "ssnew" it seems to skip the first two characters. In the debugger, for example, it is showing that the stringstream has "001.111.62.2003... etc". However, after the first "ssnew >> bValid" bValid becomes "true" and dValue becomes "0.111" and dTime becomes "0.62" indicating that the first two zeros in the stream are being ignored. Why isn't it starting at the beginning of the stream?
Cheers,
Seth
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试:
Try:
您的原始代码不起作用的原因是提取是贪婪的,因此
ssnew >>> bValid
吞噬了“001”。请注意,strstream 已被弃用,取而代之的是 stringstream。
The reason your original code didn't work is that the extraction was greedy, so
ssnew >> bValid
gobbled up the "001".Note that strstream is deprecated in favor of stringstream.