Faced with the problem of reading the structure. When reading into a structure from a file, I get an incorrect value for the size of the file itself, which is indicated in the header. First of all, I checked it without structures, that is, I simply moved the pointer and read 4 bytes of information:
//пример seekp(2); imgf.read((char*)&buf, sizeof(int)); What gave me the correct file size. But it is much more convenient to implement all this through structures that I don’t have to do right. What is my mistake?
img.h file:
#ifndef IMG_H #define IMG_H class img { public: img(char *); ~img(); // Заголовок файла typedef struct tagBITMAPFILEHEADER { unsigned short bfType; unsigned int bfSize; unsigned short bfReserved1; unsigned short bfReserved2; unsigned int bfOffBits; } BITMAPFILEHEADER; void read_bitmap_f(BITMAPFILEHEADER &); }; #endif // IMG_H img.cpp file:
#include "img.h" #include <fstream> std::fstream imgf; img::img(char * name_of_img_) { imgf.open(name_of_img_); } void img::read_bitmap_f(BITMAPFILEHEADER &buf) { imgf.read((char*)&buf, sizeof(buf)); } img::~img() { imgf.close(); } main.cpp file:
#include <iostream> #include "img.h" int main() { img * imgf = new img("lena512.bmp"); img::BITMAPFILEHEADER buf; imgf->read_bitmap_f(buf); std::cout << buf.bfSize << std::endl; // Выдает 4, хотя на самом деле изображение весит около 243КБ. return 0; }