Visual Studio std :: stringstream pubsetbuf不工作 [英] Visual Studio std::stringstream pubsetbuf does not work

查看:554
本文介绍了Visual Studio std :: stringstream pubsetbuf不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

pubsetbuf std :: stringbuf的成员在Visual Studio 2010中根本无效!

pubsetbuf member of std::stringbuf is not working at all in Visual Studio 2010!

代码:

char *FileData = ... ;
unsigned long long FileDataLen = ... ;
std::stringstream *SS = new std::stringstream(std::stringstream::in | std::stringstream::out);
SS->rdbuf()->pubsetbuf( FileData, (std::streamsize)FileDataLen );

pubsetbuf 在Visual Studio中不执行任何操作!

pubsetbuf does nothing in Visual Studio!!!

解决方法#1

std::stringstream *SS = new std::stringstream( std::string(FileData, (size_type)FileDataLen ) ),std::stringstream::in | std::stringstream::out);

解决方法#2

SS->rdbuf()->sputn(FileData, (streamsize)FileDataLen);

但这两种解决方法都会产生不必要的内存复制。
我肯定需要一个工作的 pubsetbuf 成员std :: stringbuf。

But both of these workarounds produce unnecessary memory copying. I definitely need a working pubsetbuf member of std::stringbuf.

推荐答案

code> putsetbuf 只对 fstream 才有意义(技术上, std :: basic_filebuf ),其中缓冲区和流是两个不同的东西。

putsetbuf only makes sense for fstream (technically, for std::basic_filebuf), where the buffer and the stream are two different things.

对于 stringstream c $ c> std :: basic_stringbuf )它们是一个相同的std :: string。

For stringstream (technically, std::basic_stringbuf) they are one and the same std::string.

如果你需要一个工作流一个外部字符串,考虑 std :: strstream :或 boost :: iostreams :: array_sink

If you need a stream that works on a string that's external to it, consider std::strstream: or boost::iostreams::array_sink

这篇关于Visual Studio std :: stringstream pubsetbuf不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆