• 11
• 9
• 10
• 9
• 11

# How do I re-size a binary file?

This topic is 899 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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;

// length of old file
long length = 9972608;

// allocate memory for file content

// 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++)
{
}

// 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)
{

}
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


##### Share on other sites
I'm a bit confused to what exactly you are trying to do.

You are starting with some bik files, which I assume are Bink video files. Then you are chopping these files at some location and assuming the output file will work for something.

That seems fishy at best, but we'll assume you know your data files and the reason you are slicing it at those locations.

This seems buggy:
long newSize = size - (length - size2);
char* writeBuffer = new char[newSize];

You are taking the size of a data file, and subtracting from it the difference between your hard-coded value and your second data file size. Then using that for the size of a buffer, since for some reason you like storing the complete data files in memory.

Your writing code has some fishy conditions, repeatedly checking if each of the numbers -- I assume this is around 2 million to 8 million times -- to verify if it has crossed the threshold. You've got several interesting boundary conditions in the middle, and I'm not certain they do as you expect. But since I don't know what you really expect, I cannot be certain.

It seems to me you need to simplify. Calculate the size of the block you need from the first file. Calculate the size of the block you need from the second file. Then do a single call to a copy or write function to copy those millions of bytes (rather than millions of loops of single byte copy) to get the data from that first file. Then to a single call to a copy or write function to cope those millions of bytes to get the data from that second file.

That will make it much easier logic and will likely identify your issue.

In more code-like terms:

writeBufferPosition = (whatever writeBuffer[index] starts at, or whatever you expected it to really be. );
writeBufferPosition += lengthFromBuffer2;
writeBufferPosition += lengthFromBuffer1;

Far simpler logic.

##### Share on other sites

Still can't just post the whole thing?

That is so frustrating

##### Share on other sites

Why are you trying to resize "Batman Arkham Asylum" assets?

Edited by ByteTroll

##### Share on other sites

Why are you trying to resize "Batman Arkham Asylum" assets?

That is interesting...

##### Share on other sites

Why are you trying to resize "Batman Arkham Asylum" assets?

Haven't you heard? It has a 12gb RAM requirement. Clearly OP is trying to fix that.

/troll

##### Share on other sites

It has a 12gb RAM requirement.

Would have been more funny if ByteTroll didn't say Arkham Asylum, since you're referring to Arkham Knight.

/pedant

##### Share on other sites

It has a 12gb RAM requirement.

Would have been more funny if ByteTroll didn't say Arkham Asylum, since you're referring to Arkham Knight.

/pedant

I don't care, I still found it funny

##### Share on other sites

Haven't you heard? It has a 12gb RAM requirement. Clearly OP is trying to fix that.

/troll

See. This is why you don't load the entire thing into memory first, just do it in chunks:

max_block_size = 8*1024*1024; // 8 MiB
buffer = alloc max_block_size
in_data = ...
target_pos = ...
target_next_pos = ...

# copy left side
remaining = target_pos
while (block_sz = (min remaining, max_block_size)) > 0:
write out_file, buffer, block_sz
remaining -= block_sz

write out_file, in_data, (sizeof in_data)

# copy right side
while (block_sz = read in_file, buffer, max_block_size) > 0:
write out_file, buffer, block_sz

See? Ansd yoou;re done.

EDIT:
IF you don't have enough ram, then perhaps you should download more ram instead. Edited by fastcall22

##### Share on other sites

Why are you trying to resize "Batman Arkham Asylum" assets?

After half an hour in the chat begging him to explain what he was doing I still don't really know. I think he's trying to make some kind of content mod. He said something about trying to put a riddler video in the place of a batman video, but "it was off by 8 places". I tried to get him to post the whole code body but after three rounds of that I gave up and left.