I read from the .BMP file an array of pixels that is correct in size, according to the file header.

 void img::read_bimg(char ** &smth, unsigned int bfOfBits, unsigned int biX, unsigned int biY) // biX, biY (512) - высота и ширина в //пикселях, в соответствии с заголовком файла. // bfOFBits (1078) - начало пикселей в файле. { imgf.seekg(bfOfBits, std::ios::beg); std::cout << imgf.tellg() << std::endl; // Выдает 1078, что верно. // С этого места начинаются сами пиксели. for (unsigned int y = 0 ; y < biY ; y++) { for (unsigned int x = 0; x < biX ; x++) { unsigned char buf; imgf.read((char*)&buf, sizeof(char)); smth[y][x] = buf; } } std::cout<< imgf.tellg() << std::endl; // -1 } 

I always returned -1 when tellg , which is why I thought that was the end of the file, that is, the eofbit flag. But checking with a simple move:

 imgf.seekg(0, std::ios::end); imgf.tellg; 

Got a value of 263222 , which is slightly more (512*512) . So the end of the file can not be, most likely it is a failbit . But what could the program have encountered in a file such that an error flag was set in the stream?

As well as using the clear() crutch, I drop the flag and get rid of this error .


UPD : The file was opened without the binary flag.

  • The piece of code itself is generally valid, the question is that this method gets to the input. For example, incorrect biX or biY. - Vladimir Gamalyan
  • I indicated: biX, biY (512) . - D Thr.
  • Just in case, rows of pixels in bmp are aligned to a size multiple of 4 bytes. Those. Your code will work correctly only if the width of the image is a multiple of four (a pixel, so I understand you have 8 bits in bmp). - Vladimir Gamalyan
  • @VladimirGamalian, yes, eight-bit. 512 is a multiple of 4. - D Thr.
  • one
    If the file is opened in text mode under Windows, if bytes 0x0D 0x0A occur in the file, they will be replaced with 0x0A, i.e. useful file size will be reduced, and the end will come sooner than expected. - Vladimir Gamalyan

0