我想并行读取压缩文件。我需要定义全局istream(将其引用传递给read函数)。这是我的代码。
#include <fstream>
#include <iostream>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/gzip.hpp>
std::istream &instream;
int main(int argc, char** argv) {
std::ifstream file("test.gz", std::ios_base::in |std::ios_base::binary);
boost::iostreams::filtering_streambuf<boost::iostreams::input> inbuf;
inbuf.push(boost::iostreams::gzip_decompressor());
inbuf.push(file);
instream(&inbuf);
//do something ...
file.close();
}
我也试过这个:
std::istream tmp(&inbuf);
instream = tmp;
似乎没什么用。如何在main函数中初始化全局istream变量?
答案 0 :(得分:1)
您可以从std :: istream派生并附加缓冲区:
def f1 map do
%{"f1": map["a1"]+2}
end
可能更好,使用函数:
// Omitting boost in this example
#include <iostream>
class global_istream : public std::istream
{
public:
global_istream() {};
};
// No reference
global_istream instream;
int main(int argc, char** argv) {
// Attach a buffer to the global stream.
// Any usage of instream before the buffer has been attached is fatal.
instream.rdbuf(std::cin.rdbuf());
// In your case: instream.rdbuf(&inbuf);
//do something ...
std::string s;
instream >> s;
std::cout << s << '\n';
// Detttach the buffer from the global stream.
// Any usage of instream after the buffer has been detached is fatal.
instream.rdbuf(nullptr);
}