0
我解析一个位图头文件(只是为了好玩),而且我无法将数据放入结构体中。这是我的代码:把数组的部分放入结构体
#include <iostream>
#include <fstream>
using namespace std;
struct bmp_header {
char16_t id;
int size;
char16_t reserved1;
char16_t reserved2;
int offset_to_pxl_array;
} bmp_header;
int main(int argc, char *argv[]) {
if (argc < 2) {
cout << "No image file specified." << endl;
return -1;
}
ifstream file(argv[1], ios::in|ios::binary|ios::ate);
streampos f_size = file.tellg();
char *memblock;
if (!file.is_open()) {
cout << "Error reading file." << endl;
return -1;
}
memblock = new char[f_size];
//Read whole file
file.seekg(0, ios::beg);
file.read(memblock, f_size);
file.close();
//Parse header
//HOW TO PUT FIRST 14 BYTES OF memblock INTO bmp_header?
//Output file
for(int x = 0; x < f_size; x++) {
cout << memblock[x];
if (x % 20 == 0)
cout << endl;
}
cout << "End of file." << endl;
delete[] memblock;
return 0;
}
如何把memblock的第14个元素融入bmp_header?我一直在尝试在网络上进行一些搜索,但对于这样一个简单的问题,大多数解决方案似乎都有点复杂。
还有另外一个警告 - 这将仅在小端架构工作。在一个大端的程序中,你还需要处理字节交换。 –
谢谢你的回答。有更容易的方法吗? – Pelle