[英]Getting desired binary data ranges from std::istreambuf_iterator and std::ifstream
#include <iostream>
#include <fstream>
#include <iterator>
#include <vector>
int main()
{
std::ifstream file("data.bin", std::ios::binary );
if( file.fail() )
{
std::cout << "File does not exist or could not open file";
return 0;
}
std::vector<short> buffer;
std::copy(
std::istreambuf_iterator<char>( file ),
std::istreambuf_iterator<char>(),
std::back_inserter( buffer )
);
return 0;
}
This only gives me ranges of char values (-128, 128). 这只给出了char值范围(-128,128)。
I thought using istreambuf_iterator<short>
would give me what I want but it throws an "invalid conversion" error. 我以为使用
istreambuf_iterator<short>
会给我我想要的但是它会引发“无效转换”错误。
What can I do to read binary values that are in the short
range? 我该怎么做才能读取
short
二进制值?
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>>
. 类
std::istreambuf_iterator<cT>
遍历从std::basic_istream<cT, std::char_traits<cT>>
提取的字符std::basic_istream<cT, std::char_traits<cT>>
或者实际上,它的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>
. 也就是说,给定特定的流类型,
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. 另请注意,尽管传递给流缓冲区的
std::ios_base::binary
操作,IOStream层并不打算对二进制文件进行操作。
If you want to extract short
s (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. 如果要提取
short
(或者流的字符类型之外的任何其他类型),您需要自己创建一个合适的迭代器,该迭代器将所选的二进制格式考虑在内。 This iterator can be built in terms of std::istreambuf_iterator<char>
or directly access the std::streambuf
. 这个迭代器可以用
std::istreambuf_iterator<char>
构建,也可以直接访问std::streambuf
。
Here's my recommended Spirit approach: 这是我推荐的Spirit方法:
#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.: 当然,还有
qi::dword
, qi::qword
,big-endian / little-endian variatations等:
You can look at wchar_t 你可以看看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. 我不太确定wchar_t的实际大小是否是实现定义的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.