C++ 倾销一个C++;数组到具有特定尾数的文件中
我有如下几点:C++ 倾销一个C++;数组到具有特定尾数的文件中,c++,boost,endianness,C++,Boost,Endianness,我有如下几点: boost::uint32_t data[ 256 ]; fillMyArray( data ); std::ofstream output( "this.raw", std::ios_base::out | std::ios_base::binary | std::ios_base::trunc ); output.write( reinterpret_cast< char * >( & data ), 256 * 4 ); output.close( )
boost::uint32_t data[ 256 ];
fillMyArray( data );
std::ofstream output( "this.raw", std::ios_base::out | std::ios_base::binary | std::ios_base::trunc );
output.write( reinterpret_cast< char * >( & data ), 256 * 4 );
output.close( );
boost::uint32_t数据[256];
fillMyArray(数据);
std::of流输出(“this.raw”,std::ios_base::out | std::ios_base::binary | std::ios_base::trunc);
输出写入(重新解释转换(&data),256*4);
output.close();
但是,该数组将以本地endianness保存。如何确保它与little endian一起存储?如果更简单的话,我可以使用Boost库。虽然它不会在一次调用中写入所有数据,但您可以使用,例如,写入值:
std::transform(std::begin(data), std::end(data)
std::ostream_iterator<uint32_t>(output),
[](const uint32_t& value) -> uint32_t {
return convert_endianess(value);
});
std::transform(std::begin(数据),std::end(数据)
std::ostream_迭代器(输出),
[](常数uint32\u t和值)->uint32\u t{
返回convert_endianess(值);
});
其中,
convert\u endianess
函数是进行endianess转换的函数。我认为这可以分为三个问题阶段:
endian.hpp
,为大多数设置提供此功能-它定义了boost\u BIG\u endian
或boost\u LITTLE\u endian
在2的情况下,迭代缓冲区并就地复制或转换。大多数编译器都有一个内置函数来交换数据字节,MSVC提供了\u byteswap\u ulong
,GCC提供了\u内置的bswap32
——对于其他编译器,请查看它们各自的文档
对于第3部分,如果byteswap“就地”完成,则不需要更改。如果它是一个副本,显然字节交换的数据应该提供给
write
约阿希姆·皮莱堡给出了一个很好的答案。除了编写自己的函数之外,还可以使用std::reverse(),如下所述
使用
karma::big\u word
或karma::little\u word
可能是一种选择:
查看它
/a.out&&xxd this.raw的输出
:
0000000: f000 f001 f002 f003 f004 f005 f006 f007 ................
0000010: f008 f009 f00a f00b f00c f00d f00e f00f ................
0000020: f010 f011 f012 f013 f014 f015 f016 f017 ................
0000030: f018 f019 f01a f01b f01c f01d f01e f01f ................
0000040: f020 f021 f022 f023 f024 f025 f026 f027 . .!.".#.$.%.&.'
// ...
0000200: 00f0 01f0 02f0 03f0 04f0 05f0 06f0 07f0 ................
0000210: 08f0 09f0 0af0 0bf0 0cf0 0df0 0ef0 0ff0 ................
0000220: 10f0 11f0 12f0 13f0 14f0 15f0 16f0 17f0 ................
0000230: 18f0 19f0 1af0 1bf0 1cf0 1df0 1ef0 1ff0 ................
0000240: 20f0 21f0 22f0 23f0 24f0 25f0 26f0 27f0 .!.".#.$.%.&.'.
完整代码:
#include <boost/spirit/include/karma.hpp>
#include <fstream>
namespace karma = boost::spirit::karma;
template <typename C>
void fillMyArray(C& data)
{
std::iota(begin(data), end(data), 0xf000);
}
int main()
{
std::vector<boost::uint32_t> data(256);
fillMyArray(data);
std::ofstream output( "this.raw", std::ios_base::out | std::ios_base::binary | std::ios_base::trunc );
boost::spirit::karma::ostream_iterator<char> outit(output);
karma::generate(outit, +karma::big_word, data);
karma::generate(outit, +karma::little_word, data);
}
#包括
#包括
名称空间业力=提升::精神::业力;
模板
无效填充MyArray(C和数据)
{
标准::物联网(开始(数据),结束(数据),0xf000);
}
int main()
{
std::矢量数据(256);
fillMyArray(数据);
std::of流输出(“this.raw”,std::ios_base::out | std::ios_base::binary | std::ios_base::trunc);
boost::spirit::karma::ostream_迭代器outit(输出);
因果报应::生成(outit,+因果报应::大字,数据);
因果报应::生成(outit,+因果报应::小单词,数据);
}
为什么不简单地循环数组中的数据,并用正确的尾数写入每个值?@JoachimPileborg:部分问题是要确定什么是“正确的”,如果机器是bigendian,则必须“向后”写入……您是否试图写入整个数组?因为实际字节数是256*sizeof(uint32\t)。或者你认为你在铸造数组中的每个元素?因为这里不是这样的。@davmac我忘记了*4
,谢谢。那std::ostream\u迭代器
的什么实例会。。。be?`ostream\u迭代器是我上次检查的类模板。即使你让它编译,这个流也会包含字符串化的数字,对吗?@sehe啊,要理解你的意思需要一点思考。更新答案:)现在,我仍然认为,如果您使用了ostreambuf\u迭代器,这将只适用于非标准的basic\u of stream
。但是,由于对实现的区域设置限制,这可能会发生故障或不可移植,或者您可以让Boost为您做所有这些:Karma做了,Boost序列化也做了。看看我的答案。