1

for a school project, we have in a certain function read pixels from a binary file, which is a BMP format. pixels is a 2D array of Pixel type from an object. The goal here is to read from the binary file X lines and Y columns of pixels. However, there is some padding after each line of pixels, which can be calculated with the function calculatePadding(), which has a return type of unsigned.

What we have to do is read line by line the file in binary and skip the padding after each line, as we are storing the pixels in another variable. This is what I did so far:

for (int i = image.height - 1; i >= 0; i--) {
    for (unsigned j = 0;j < image.width; j++) {
        file.read((char*)&image.pixels[i][j], sizeof(Pixel));
    }
    fichier.seekg(sizeof(calculatePadding(image)) + tellp() , ios::beg);
}

My problem is with the last line(before the last closing bracket). I know I have to set the cursor forward by adding the current position of the cursor and whatever size the padding takes. However, how can I add an unsigned and streampos? I don't understand what conversion I should make. We are not allowed to use ignore()

Thank you for any help!

Also, on a side note, how can you write '0's to a binary file? On the next function, I have to take an image and write it in a binary file and put '0's in binary where there is padding.

Is this something that works?

int zero = 0;
file.write((char*)&zero, sizeof(calculatePadding(image)));
will smith
  • 77
  • 1
  • 2
  • 9

2 Answers2

0

afaik there is no portable way to sum/subtract streampos and numbers. but it's possible to achieve the same in two steps: 1. set position relative to the beginning (with streampos) 2. set position relative to the current position (with unsigned)

std::ofstream ofs;
ofs.open("out.txt");
ofs<<"test string\n";
auto opos = ofs.tellp();
std::ifstream ifs;
ifs.open("in.txt");
ifs.seekg(opos, std::ios::beg);
ifs.seekg(10/* your additional offset*/, std::ios::cur);

another possibility - sum streampos with streamoff

ifs.seekg(opos + std::streamoff(10/* your additional offset*/), std::ios::beg);

about 0. you can use put /write methods to write one or several bytes (chars). your approach is incorrect. you have only sizeof(int) zeros but you are trying to use the size that can be larger. there is a similar question - How to write 'n' copies of a character to ostream like in python

Alexander
  • 698
  • 6
  • 14
0
sizeof(calculatePadding(image))//?

sizeof operator is definitely wrong. sizeof(n) where n is any integer is always constant, it's usually 4 or 8 depending on the operating system.

Padding in this case is between 0 and 4 bytes for a 24bit bitmap (0,1,2,3), it should be the same as image.width % 4. You just need to skip that many bytes after reading each row:

int padding = calculatePadding(image);
for (int i = image.height - 1; i >= 0; i--) {
    for (unsigned j = 0;j < image.width; j++) {
        file.read((char*)&image.pixels[i][j], sizeof(Pixel));
    }
    file.seekg(padding, ios::cur);
}

To add padding to the output file use:

char buf[4]={0};
fileout.write(buf, padding);

This is assuming padding is never equal or greater than 4, or less than zero.

Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77