Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Fstream 从ostringstream传递到istringstream和文件_Fstream_Stringstream_Streambuf - Fatal编程技术网

Fstream 从ostringstream传递到istringstream和文件

Fstream 从ostringstream传递到istringstream和文件,fstream,stringstream,streambuf,Fstream,Stringstream,Streambuf,我拼命尝试在ostringstream中写入数据,然后在其他对象的istringstream或文件中传输数据 std::ostringstream oss; oss << "Hello World"; std::ostringstream oss; oss您误会了-执行iss.basic_ios::rdbuf(oss.rdbuf())只会更改输入字符串流的内部指针以指向其他输出字符串流的缓冲区。它对iss的内部缓冲区的内容没有影响(即没有数据传输) 据我所知,输入字符串流的get区

我拼命尝试在ostringstream中写入数据,然后在其他对象的istringstream或文件中传输数据

std::ostringstream oss;
oss << "Hello World";
std::ostringstream oss;

oss您误会了-执行
iss.basic_ios::rdbuf(oss.rdbuf())
只会更改输入字符串流的内部指针以指向其他输出字符串流的缓冲区。它对
iss
的内部缓冲区的内容没有影响(即没有数据传输)


据我所知,输入字符串流的get区域可能是无效的,并且完全由它的实现来使用。我不认为有任何方法可以改变这一点。

那么,在iss中将streambuf光标设置为开始是否有趣?(即使在oss继续使用时设置streambuf可能很危险)或者我必须将stringstream复制为:iss.str(oss.str())和ofs@user2928422,我认为后者是最合理和最直接的解决方案,所以是:
ofs
iss.basic_ios<char>::rdbuf(oss.rdbuf());
std::ofstream ofs("test.txt");
ofs << oss.rdbuf();
ofs.close();