修改c ++中basic_ostream对象的内容,或删除basic_ostream对象的数据内容 [英] Modify contents of basic_ostream object in c++ or, deleting data contents of basic_ostream object

查看:397
本文介绍了修改c ++中basic_ostream对象的内容,或删除basic_ostream对象的数据内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个函数中得到一个 ostream 对象引用。它包含一个我需要修改的字符串。
我使用 rdbuf()函数将 ostream 的内容复制到 stringstream
现在我需要将更新的字符串(基础字符串)复制回ostream对象。我如何做到这一点?

I get a ostream object reference in a function. It contains a string which I need to modify. I copied the contents of the ostream using rdbuf() function into a stringstream. Now I need to copy the updated stringstream (the underlying string that is) back to the ostream object. How can I do this?

我搜索了清除ostream内容,但找不到的方法。

I searched ways of erasing the contents of the ostream, but could not find one.

注意:我不能改变其他函数的实现,即我无法使用 ostringstream (我知道可以解决我的问题)。

Note: I cannot change the implementation of other functions, i.e., I an unable to use ostringstream (which I know can solve my problem).

推荐答案

您可以尝试使用 ostream :: seekp()或 streambuf :: pubseekpos()

std::ofstream out;

out.seekp(-10, std::ios_base::cur); // move back 10 positions from current location
out.rdbuf()->pubseekpos(12); // move to position 12

请记住:


  • 如果数据的底层目标不支持seek,这可能会失败(例如,streambuf可能实际上将数据写入套接字)

  • 如果它适用于你的特定类型的流,你必须保持计数的位置和长度,以及你覆盖多少(如果新的值有一个不同的长度,你想要替换的流中的旧值)。

这篇关于修改c ++中basic_ostream对象的内容,或删除basic_ostream对象的数据内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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