I am trying to read a binary file and add a sequence of four little-endian bytes into an int, but for some reason one of the bytes is being read with some kind of error. The program is a save file editor, and the error only happens after I edit one of the files playtime (seconds*60) to some big number (I used 7075920 for the example below).
Here's the code of the reading part:
char buffer[4];
std::ifstream saveFile (ui->directoryLine->text().toStdString(), std::ios::binary | std::ios::in);
saveFile.seekg(saveSlot*2416+44); //Read playtime and convert
saveFile.read(buffer, 4);
playtime = (buffer[0] | (buffer[1]<<8) | (buffer[2]<<16) | (buffer[3]<<24))/60;
ui->hSpinBox->setValue(playtime/3600);
ui->mSpinBox->setValue(playtime/60 % 60);
ui->sSpinBox->setValue(playtime % 60);
When I write the playtime as 7075920 on the file (hex 6BF850) and I view the file using a hex editor, the bytes are set correctly to 50 F8 6B 00 (little-endian)
But using printf with %X to print the buffer after reading the modified file returns this: 50 FFFFFFF8 6B 0, and playtime is then calculated as -32
I don't understand how the program is working with unmodified small int files, but returning broken bytes for modified ones if they have no errors and the diff tool of the hex editor shows that the only differences between the modified file and the original one (that works) are on those four modified bytes.