stringstream缓冲区操作
std::stringstream buffer manipulation
我正在将一些数据放入流中,但从stringstream
获得std::stringstream data;
auto buf = data.rdbuf();
buf->sputn(XXX);
我想要的是能够把一些虚拟数据放入这个缓冲区,然后在以后的时间,一旦我有正确的数据,替换虚拟数据。
下面这些行:
auto count = 0;
buf->sputn((unsigned char *)&count, sizeof(count));
for (/*some condition*/)
{
// Put more data into buffer
// Keep incrementing count
}
// Put real count at the correct location
我尝试使用pubseekpos + sputn,但它似乎不像预期的那样工作。有什么正确的方法吗?
只使用data.seekp(pos);
然后data.write()
-你根本不需要填充缓冲区
这可以帮助您开始,它写入一些X
并将它们打印回来,这也可以通过data << 'X'
:
#include <sstream>
#include <iostream>
int main() {
std::stringstream data;
auto buf = data.rdbuf();
char c;
for (int count = 0; count < 10; count++) {
buf->sputn("X", 1);
}
while (data >> c) {
std::cout << c;
}
return 0;
}
相关文章:
- 为什么在popback()操作之后,它仍然打印完整的矢量
- C++字符*缓冲区的大小
- 重载操作程序时出错>>用于类中的字符串 memebr
- 无法对套接字执行操作,因为系统缺少足够的缓冲区空间或队列已满
- 安全操作以清除C++中的空输入缓冲区
- QML:读取由计算着色器操作的缓冲区
- 操作方法:将 boost::endian 缓冲区类型转换回本机格式
- 在Win32管道上将两个缓冲区合并为一个写操作
- 在不使用缓冲区的情况下对2个图像进行操作(加法、减法等)
- 在MS Visual Studio 2010上使用带有Cuda互操作的OpenGL缓冲区时出现异常错误
- 从 C++ 中的函数操作字符数组缓冲区
- C++缓冲区操作
- 如何直接操作终端输出缓冲区
- 是否需要为C/C++中的文件I/O操作创建自己的缓冲区
- 这样操作cv::Mat的内部缓冲区安全吗
- 使用核心配置文件交换缓冲区使用了无效操作
- DX11 OpenGL 互操作:无法复制缓冲区
- stringstream缓冲区操作
- WriteFile成功,即使缓冲区被挂起的操作覆盖
- 矩阵乘以矢量操作与原始缓冲区在本征