如何使用boost :: iostreams :: filtering_wistream读取UTF-16LE中的gz文件?

时间:2018-09-12 14:09:14

标签: c++ boost

我有一个用UTF16-LE编写的文件test.txt,其内容如下:

ab
c

现在,我用gz对其进行压缩,并得到了一个新文件test.txt.gz,我想读取此gz文件并获取原始内容(分别为abc )。

我决定使用boost::iostreams::filtering_wistream来做。这是我尝试过的。

#include <iostream>
#include <string>
#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <codecvt>

int main() {
    std::wifstream wInputFileStream("/tmp/test.txt.gz");
    wInputFileStream.imbue(std::locale(wInputFileStream.getloc(), new std::codecvt_utf16<
            wchar_t, 0x10ffff, static_cast<std::codecvt_mode>(std::little_endian | std::consume_header)>));

    boost::iostreams::filtering_wistream wFilteringInputStream;
    wFilteringInputStream.push(boost::iostreams::gzip_decompressor());
    wFilteringInputStream.push(wInputFileStream);

    wchar_t c;
    wInputFileStream.get(c); // trying to get the first character in the file, as a test
    std::cout << c << std::endl;

    return 0;
}

但是它不起作用,我有很多错误,这里有一些错误。

In file included from /usr/include/boost/iostreams/filtering_streambuf.hpp:17,
                 from /usr/include/boost/iostreams/filtering_stream.hpp:22,
                 from /home/searene/CLionProjects/cmake-test/hello.cpp:6:
/usr/include/boost/iostreams/chain.hpp: In instantiation of ‘void boost::iostreams::detail::chain_base<Self, Ch, Tr, Alloc, Mode>::push_impl(const T&, std::streamsize, std::streamsize) [with T = boost::iostreams::basic_gzip_decompressor<>; Self = boost::iostreams::chain<boost::iostreams::input, wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >; Ch = wchar_t; Tr = std::char_traits<wchar_t>; Alloc = std::allocator<wchar_t>; Mode = boost::iostreams::input; std::streamsize = long int]’:
/usr/include/boost/iostreams/chain.hpp:205:5:   required from ‘void boost::iostreams::detail::chain_base<Self, Ch, Tr, Alloc, Mode>::push(const T&, std::streamsize, std::streamsize, typename boost::disable_if<boost::iostreams::is_std_io<T> >::type*) [with T = boost::iostreams::basic_gzip_decompressor<>; Self = boost::iostreams::chain<boost::iostreams::input, wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >; Ch = wchar_t; Tr = std::char_traits<wchar_t>; Alloc = std::allocator<wchar_t>; Mode = boost::iostreams::input; std::streamsize = long int; typename boost::disable_if<boost::iostreams::is_std_io<T> >::type = void]’
/usr/include/boost/iostreams/chain.hpp:496:7:   required from ‘void boost::iostreams::detail::chain_client<Chain>::push_impl(const T&, std::streamsize, std::streamsize) [with T = boost::iostreams::basic_gzip_decompressor<>; Chain = boost::iostreams::chain<boost::iostreams::input, wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >; std::streamsize = long int]’
/usr/include/boost/iostreams/chain.hpp:484:5:   required from ‘void boost::iostreams::detail::chain_client<Chain>::push(const T&, std::streamsize, std::streamsize, typename boost::disable_if<boost::iostreams::is_std_io<T> >::type*) [with T = boost::iostreams::basic_gzip_decompressor<>; Chain = boost::iostreams::chain<boost::iostreams::input, wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >; std::streamsize = long int; typename boost::disable_if<boost::iostreams::is_std_io<T> >::type = void]’
/home/searene/CLionProjects/cmake-test/hello.cpp:16:69:   required from here
/usr/include/boost/iostreams/chain.hpp:258:9: error: no matching function for call to ‘std::__cxx11::list<boost::iostreams::detail::linked_streambuf<wchar_t, std::char_traits<wchar_t> >*, std::allocator<boost::iostreams::detail::linked_streambuf<wchar_t, std::char_traits<wchar_t> >*> >::push_back(std::unique_ptr<boost::iostreams::stream_buffer<boost::iostreams::basic_gzip_decompressor<>, std::char_traits<wchar_t>, std::allocator<wchar_t>, boost::iostreams::input>, std::default_delete<boost::iostreams::stream_buffer<boost::iostreams::basic_gzip_decompressor<>, std::char_traits<wchar_t>, std::allocator<wchar_t>, boost::iostreams::input> > >::pointer)’
         list().push_back(buf.get());
         ^~~~

我做错了什么?如何运作?


关于重复项:

我已经检查过this question,这与我的不同。这个问题与如何写作有关,而我的与阅读有关。另外,最重要的是,当我要读取的文件很大时,我无法将所有数据都保存在变量中。但是对于这个问题,这不是问题,因为它已经有一个wstring来保存所有数据。因此,该问题的答案对我也无济于事。

0 个答案:

没有答案