g++ 4.6 stringstream 的错误?
我过去曾在单个线程中使用 stringstream 作为管道(使用 g++ 4.5),没有任何问题。现在我尝试了相同的方法,但我无法让它在 g++ 4.6 中工作:问题是内部 streambuf
永远不会填充到第一个字节之外。
考虑这段代码,
#include <iostream>
#include <sstream>
using namespace std;
int main(){
stringstream pipe(ios_base::in|ios_base::out|ios_base::binary);
const char* in="lol";
pipe.write(in, 4);
char out[4]={0};
cout<<pipe.readsome(out, 4)<<" "<<out<<endl;
}
它意外地打印出“1 l”。
我对 stringstream 的使用有明显的错误吗?否则,我必须得出结论,g++ 4.6 STL 存在一些缺陷。
聚苯乙烯 我使用带有关联 stringbuf 的 iostream 得到了相同的结果,这基本上就是 stringstream 所做的。
I have used stringstream as pipes within a single thread in the past (with g++ 4.5) with no problem. Now I tried the same approach, but I can't get it to work in g++ 4.6: the problem is that the internal streambuf
is never filled beyond the first byte.
Consider this code
#include <iostream>
#include <sstream>
using namespace std;
int main(){
stringstream pipe(ios_base::in|ios_base::out|ios_base::binary);
const char* in="lol";
pipe.write(in, 4);
char out[4]={0};
cout<<pipe.readsome(out, 4)<<" "<<out<<endl;
}
This prints unexpectedly "1 l".
Is there any evident error in my usage of stringstream? Otherwise, I must conclude that there's some flaw in g++ 4.6 STL.
P.S.
I have the same results using a iostream with an associated stringbuf, which is basically what stringstream does.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我重新阅读了规范,最后我决定使用这段代码来检索 stringstream 管道中的可用字节:
pipe.tellp()-pipe.tellg()
。据我所知,它符合一切要求,因此可以保证始终正常工作。
I re-read the specs, and at last I decided to use this piece of code to retrieve the available bytes in the stringstream pipe:
pipe.tellp()-pipe.tellg()
.As far as I know, it's compliant to everything, and so it's assured to work always.