g++ 4.6 stringstream 的错误?

发布于 2024-12-20 08:05:00 字数 657 浏览 4 评论 0原文

我过去曾在单个线程中使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

相思故 2024-12-27 08:05:00

我重新阅读了规范,最后我决定使用这段代码来检索 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文