Getting desired binary data ranges from std::istreambuf_iterator and std::ifstream

前端 未结 2 697
感动是毒
感动是毒 2020-12-22 03:47
#include 
#include 
#include 
#include 

int main()
{
    std::ifstream file(\"data.bin\", std::ios::bin         


        
相关标签:
2条回答
  • 2020-12-22 04:19

    Here's my recommended Spirit approach:

    #include <fstream>
    #include <boost/spirit/include/qi.hpp>
    
    namespace qi = boost::spirit::qi;
    
    int main()
    {
        std::ifstream file("data.bin", std::ios::binary);
        if(!file)
            std::cout << "File does not exist or could not open file";
        boost::spirit::istream_iterator f(file), l;
    
        std::vector<int16_t> buffer;
        bool ok = qi::parse(f, l, *qi::word, buffer);
    }
    

    Of course, there is qi::dword, qi::qword, big-endian/little-endian variatations etc.:

    • http://www.boost.org/doc/libs/1_52_0/libs/spirit/doc/html/spirit/qi/reference/binary/binary_native.html
    • http://www.boost.org/doc/libs/1_52_0/libs/spirit/doc/html/spirit/qi/reference/binary/binary_little.html
    • http://www.boost.org/doc/libs/1_52_0/libs/spirit/doc/html/spirit/qi/reference/binary/binary_big.html

    You can look at wchar_t

    #include <iostream>
    #include <fstream>
    #include <iterator>
    #include <vector>
    int main()
    {
        std::wifstream file("data.bin", std::ios::binary );
        if( file.fail() )
        {
            std::cout << "File does not exist or could not open file";
            return 0;
        }
        std::vector<wchar_t> buffer;
        std::copy( 
                std::istreambuf_iterator<wchar_t>( file ),
                std::istreambuf_iterator<wchar_t>(),
                std::back_inserter( buffer )
                );
    }
    

    I'm not too sure whether the actual size of wchar_t is implementation defined.

    0 讨论(0)
  • 2020-12-22 04:38

    The class std::istreambuf_iterator<cT> iterates over the characters extracted from a std::basic_istream<cT, std::char_traits<cT>> or, actually, its std::basic_streambuf<cT, std::char_traits<cT>>. That is, given a specific stream type, there is no choice for the std::istreambuf_iterator<cT>. Also note that the IOStream layer is not intended to operate on binary files, despite the std::ios_base::binary operation which is passed to the stream buffers.

    If you want to extract shorts (or any other type than the stream's character type) you'd need to create a suitable iterator yourself which takes the chosen binary format the file was written in into account. This iterator can be built in terms of std::istreambuf_iterator<char> or directly access the std::streambuf.

    0 讨论(0)
提交回复
热议问题