c++ - Visual Studio std::stringstream pubsetbuf 不起作用

标签 c++ visual-studio stl std

std::stringbuf 的

pubsetbuf 成员在 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 中什么都不做!!!

解决方法#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);

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

最佳答案

putsetbuf 仅对 fstream 有意义(从技术上讲,对 std::basic_filebuf 而言),其中缓冲区和流是两个不同的东西.

对于 stringstream(技术上,std::basic_stringbuf)它们是同一个 std::string。

如果您需要一个在其外部字符串上工作的流,请考虑 std::strstream:或 boost::iostreams::array_sink

关于c++ - Visual Studio std::stringstream pubsetbuf 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10054396/

相关文章:

c++ - 抛出 'cv::Exception' 的实例后,OpenCV 中出现错误标志错误。 cvGetMat 中不支持的数组类型

c# - 创建可重用的项目和解决方案结构

asp.net - 在ASP.NET MVC 5 Controller 方法中调试多个线程

c++ - vector map - 怎么做

c++ - 为什么 C++ 标准库容器使用内存池,如果显然 malloc/free 对做同样的工作?

c++ - 大数模幂运算

c++ - 如何在numpy c-api中共享随机数生成器?

c++ - 测量互斥量争用/解释互斥量输出

c# - TPL 中断未处理的异常

C++ std::ctype<char>::widen() 的作用是什么?