是否可以将字符串的 streambuf 直接传递给 filebuf?
Is it possible to pass the stringbuf's streambuf direct to the filebuf?
我正在研究操作字符串的函数,我需要将其保存在文件中。如果我能将 stringbuf 直接传递给 ofstream 的 filebuf,那将是一个巨大的优势。
我知道两者都使用 streambuf 对象,我可以以某种方式将 filebuf 的 streambuf 替换为我的 stringbuf 的流吗?
看看下面的代码,以更好地理解这个想法:
#include <fstream>
#include <sstream>
void printit(std::stringbuf &MyBb) {
std::ofstream fFile("newfile.txt");
fFile.rdbuf(MyBb.rdbuf()); //MyBb doesn't have this member function
//How can I give to fFile the streambuf
//from my stringbuf?
}
int main() {
std::stringbuf MyB;
MyB.sputn("First sentencen",15);
printit(MyB);
return 0;
}
你只需要像给它写一个字符串一样,然后使用你的函数。使用fFile << MyPb.rdBuff();
这可能应该有效
相关文章:
- 将 boost 序列化对象的 asio::streambuf 表示转换为 Beast 的 DynamicBody req.body()
- 自定义 std::fstream,std::filebuf 的溢出和下溢函数未为每个字符调用
- 在抛出 'std::runtime_error' 的实例后终止调用 what(): Filebuf 和 ostream 的 I/O 错误
- Writing uin32_t to asio::streambuf
- Boost::asio::streambuf consumption() 不清空缓冲区
- 字符 - 'pub' 在 std::streambuf::p ubsetbuf() 方法中是什么意思?
- 取得 streambuf/stringbuf 数据的所有权
- 将 std::string 转换为 boost::asio::streambuf
- 如何仅使用 std::filebuf 将数据(二进制模式)写入文件
- 在发送/获取结构时使用 streambuf
- Boost ASIO ForwardIterator for streambuf
- boost::asio::streambuf::consume-注入垃圾字符
- 从boost asio streambuf读取会将字节保持在缓冲区中
- 是否可以将字符串的 streambuf 直接传递给 filebuf?
- 如何访问boost :: asio :: streamBuf输入作为字符串
- 如何复制或重复使用boost :: asio :: streambuf
- 为gzipped流编写自定义streambuf的问题
- boost::asio::streambuf-如何重用缓冲区
- BOOST :: ASIO :: StreamBuf收缩到合适
- 可以将STD :: StreamBuf的自定义实现中的异常交付给流用户