Hii I'm trying to re-size a binary file. It's seems when I do this it's off by 8 bytes. If I use a different file it might be 3 or 5 bytes.
Here is my re-size code:
// index the beginning of the new .bik file
int index = 2160640;
// files to load
std::ifstream readFile("baa_logo_run_v5_h264.bik", std::ifstream::binary);
std::ifstream readFile2("riddeler.bik", std::ifstream::binary);
// length of old file
long length = 9972608;
// get size of readFile
readFile.seekg(0, readFile.end);
long size = readFile.tellg();
readFile.seekg(0);
// get size of readFile2
readFile2.seekg(0, readFile2.end);
long size2 = readFile2.tellg();
readFile2.seekg(0);
// allocate memory for file content
char* readBuffer1 = new char[size];
char* readBuffer2 = new char[size2];
// read contents in buffer
readFile.read(readBuffer1, size);
readFile2.read(readBuffer2, size2);
readFile.close();
readFile2.close();
// file to write to
std::ofstream WriteFile("myfile.ty", std::ofstream::binary); // update new bik files
// .bik file size is less than current bik length
if (size2 < length)
{
// re-size writeBuffer
long newSize = size - (length - size2);
char* writeBuffer = new char[newSize];
// rebuild file again
for (int i = 0; i < newSize; i++)
{
writeBuffer[i] = readBuffer1[i];
}
// counters and flag
int num = 0;
int num2 = 0;
int flag = 0;
// length of the whole file
for (int i = index; i < newSize; i++)
{
// length of the new bik file
if (num < size2)
{
writeBuffer[i] = readBuffer2[num];
}
num++;
// if the new bik is stored then flag = 1
if (num > size2)
{
flag = 1;
}
// write the rest of the other files.
if (flag == 1)
{
if (num2 < newSize && index + length + num2 < newSize)
{
writeBuffer[i] = readBuffer1[(index + length + num2)];
}
num2++;
}
}
// write to file
WriteFile.write(writeBuffer, newSize);
WriteFile.close();
delete[] writeBuffer;
}
// end of rebuild file