我想读取一个用 a 编写的二进制文件,QDataStream
并在 LittleEndian 中用 a 编码std::fstream
(在同一平台上,因此不关心具有不同格式的一种数据类型的问题)。
我怎样才能最好地做到这一点?据我所知,std::fstream
没有内置功能来读取/写入 LittleEndian 数据。
我深入研究了问题并发现了以下内容(伪代码):
ofstream out; //initialized to file1, ready to read/write
ifstream in; //initialized to file2; ready to read/write
QDataStream q_out; //initialized to file2; ready to read/write
int a=5, b;
//write to file1
out << a; //stored as 0x 35 00 00 00. Curiously, 0x35 is the character '5' in ASCII-code
//write to file2
q_out << a; //stored as 0x 05 00 00 00
//read from file2 the value that was written by q_out
in >> b; //will NOT give the correct result
//read as raw data
char *c = new char[4];
in.read(c, 4);
unsigned char *dst = (unsigned char *)&b;
dst[3] = c[3];
dst[2] = c[2];
dst[1] = c[1];
dst[0] = c[0];
//b==5 now
总结一下:QDataStream
以不同于std::fstream
. QDataStream
有没有一种简单的方法可以读取使用写入的二进制数据std::fstream
?