0
我不可能找到以下行为的解释:Boost.IOStreams:如何用'rdbuf'正确地重定向文件流?
#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/stream.hpp>
#include <iostream>
#include <sstream>
TEST_CASE(rdbuf) {
{
boost::iostreams::stream<boost::iostreams::file_sink> file("test");
file << "Hello, World!";
}
{
boost::iostreams::stream<boost::iostreams::file_source> file("test");
std::string line;
std::getline(file, line);
CHECK_EQUAL(line, "Hello, World!");
}
{
boost::iostreams::stream<boost::iostreams::file_source> file("test");
std::istringstream iss;
std::string line;
file.rdbuf(iss.rdbuf());
std::getline(file, line);
CHECK_EQUAL(line, "");
std::getline(iss, line);
CHECK_EQUAL(line, ""); // (1) Why?
}
{
boost::iostreams::stream<boost::iostreams::file_source> file("test");
std::ostringstream oss;
std::string line;
file.rdbuf(oss.rdbuf());
std::getline(file, line);
CHECK_EQUAL(line, "");
line = oss.str();
CHECK_EQUAL(line, ""); // (2) Why?
}
}
在这两种情况下(1)
和(2)
,我本来打算
CHECK_EQUAL(line, "Hello, World!");
成功。
我在这里错过了什么?谢谢。