我基本上是在尝试将整数添加到字符串,然后在特定时间将结束符添加到字符串,然后继续添加整数。我希望将它们全部添加到一个字符串对象中。
所以我在字符串流中添加了一个整数,将字符串流馈入了一个字符串。然后在字符串中添加“ \ n”。
然后,我向stringstream添加了另一个int,然后再次将其提供给字符串。
不幸的是,stringstream对象中的第二个对象没有被添加到字符串中,我不确定为什么不能。
我尝试将两个对象添加到我的字符串流对象中,然后一次将它们添加到字符串中(中间有换行符),但是stringstream一次将字符串中的所有内容都添加到了字符串中,因此不起作用。
int main(){
string h;
stringstream ss;
ss << 1;
ss >> h;
h += '\n';
ss << 2;
ss >> h;
h += '\n';
cout << h << "endline";
cout << endl;
return 0;
}
当然没有错误消息。我希望它输出:
1
2
endline
相反,我得到
1
endline
因此,很明显,字符串将添加我的两个结尾字符,而不是我添加到stringstream的两个字符。
最佳答案
为了演示发生了什么,我们需要检查各种流状态位。
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
string h;
stringstream ss;
if (ss << 1)
{
cout << "1 success. Fail " << ss.fail()
<< " eof " << ss.eof() << " bad " << ss.bad() << endl;
if (ss >> h)
{
cout << "2 success. Fail " << ss.fail()
<< " eof " << ss.eof() << " bad " << ss.bad() << endl;
h += '\n';
if (ss << 2)
{
cout << "3 success. Fail " << ss.fail()
<< " eof " << ss.eof() << " bad " << ss.bad() << endl;
if (ss >> h)
{
cout << "4 success. Fail " << ss.fail()
<< " eof " << ss.eof() << " bad " << ss.bad() << endl;
h += '\n';
cout << h << "endline";
cout << endl;
}
else
{
cout << "4 Fail " << ss.fail() << " eof " << ss.eof()
<< " bad " << ss.bad() << endl;
}
}
else
{
cout << "3 Fail " << ss.fail() << " eof " << ss.eof()
<< " bad " << ss.bad() << endl;
}
}
else
{
cout << "2 Fail " << ss.fail() << " eof " << ss.eof()
<< " bad " << ss.bad() << endl;
}
}
else
{
cout << "1 Fail " << ss.fail() << " eof " << ss.eof()
<< " bad " << ss.bad() << endl;
}
return 0;
}
输出量
1 success. Fail 0 eof 0 bad 0 2 success. Fail 0 eof 1 bad 0 3 Fail 1 eof 1 bad 0
So the first write succeeded. Flawless victory. But the first read read everything in the stream and hit the end of file (not that the stream is a file, but the name stuck), setting the EOF bit. Once the EOF bit is set, there isn't much you can do with a stream other than clear the bit and pray someone adds more data to be read.
More data was added to the stream, but the file could not accept it because of the EOF bit.
If we clear the EOF
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
string h;
stringstream ss;
if (ss << 1)
{
cout << "1 success. Fail " << ss.fail() << " eof " << ss.eof()
<< " bad " << ss.bad() << endl;
if (ss >> h)
{
cout << "2 success. Fail " << ss.fail() << " eof " << ss.eof()
<< " bad " << ss.bad() << endl;
h += '\n';
ss.clear(); // added this
if (ss << 2)
{
cout << "3 success. Fail " << ss.fail() << " eof " << ss.eof()
<< " bad " << ss.bad() << endl;
if (ss >> h)
{
cout << "4 success. Fail " << ss.fail() << " eof " << ss.eof()
<< " bad " << ss.bad() << endl;
h += '\n';
cout << h << "endline";
cout << endl;
}
else
{
cout << "4 Fail " << ss.fail() << " eof " << ss.eof()
<< " bad " << ss.bad() << endl;
}
}
else
{
cout << "3 Fail " << ss.fail() << " eof " << ss.eof()
<< " bad " << ss.bad() << endl;
}
}
else
{
cout << "2 Fail " << ss.fail() << " eof " << ss.eof()
<< " bad " << ss.bad() << endl;
}
}
else
{
cout << "1 Fail " << ss.fail() << " eof " << ss.eof()
<< " bad " << ss.bad() << endl;
}
return 0;
}
现在的输出
1 success. Fail 0 eof 0 bad 0 2 success. Fail 0 eof 1 bad 0 3 success. Fail 0 eof 0 bad 0 4 success. Fail 0 eof 1 bad 0 2 endline
If we ignore all the status information I added we really got
2 endline
not the desired
1 2 endline
because ss >> h
will overwrite everything already in h
. The "1\n" is wiped out by the "2"
The easiest way to get what you want is to write everything in and then get the contents as a string
.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
string h;
stringstream ss;
if (ss << 1 << '\n' << 2 << '\n')
{
cout << ss.str() << "endline";
cout << endl;
}
else
{
cout << "Fail " << ss.fail() << " eof " << ss.eof()
<< " bad " << ss.bad() << endl;
}
return 0;
}
这次的输出是
1个
2
终点线
关于c++ - Stringstream没有送入添加了结尾字符的字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58619246/