0

I am looking for a way to split an array into multiple arrays in C/C++, with standard Win32 and C/C++ functions.

Here's an example, how I read a file into the array.

using namespace std;

LPSTR File_To_Read = "FILE.exe"; 
DWORD File_To_Read_Size = NULL;
DWORD inputSize = NULL;
PBYTE inputData = NULL;

ifstream input(File_To_Read, ifstream::binary | ifstream::ate);
File_To_Read_Size = static_cast<DWORD>(input.tellg());
input.seekg(input.beg);

inputData = new BYTE[File_To_Read_Size];
input.read(reinterpret_cast<char*>(inputData), File_To_Read_Size);
input.close();

Now I want to split the inputData like this.

DWORD inputSize_part1;
DWORD inputSize_part2;
DWORD inputSize_part3;

PBYTE inputData_part1;
PBYTE inputData_part2;
PBYTE inputData_part3;

So in the later I can also put them back together.

How should I proceed? I would show an example code what I have tried, but my code would not make not much sense for you experts.

Edit: @IKH Chunk sizes should be around the same size. So if inputData is 33kb, then inputData_part1(and inputSize_part1) should be 11kb, inputData_part2(and inputSize_part2) should be 11kb, and so on. So in the end there would be 3x 11kb arrays and DWORDS for their sizes.

6
  • 1
    I would show an example code what I have tried, but my code would not make not much sense for you experts. Oh, stop it, you :$ Show us the code anyway. Commented Mar 31, 2014 at 11:09
  • chunks? what chunks? you don't specify either the size of chunk or the number of chunk. Commented Mar 31, 2014 at 11:10
  • @nouney Actually I don't have it anymore. I splitted the size in 3, then looped 3 times and tried reading always 1/3 of the array into split1, split2, split3 + their sizes but at the moment I have no idea where I should begin with. Edit: IKH edited the post. Commented Mar 31, 2014 at 11:15
  • 2
    Read into a single (dynamic) array, then treat that array as 3 different arrays: int *arr1 = fullarray; int *arr2 = fullarray + chunksize; int *arr3 = fullarray + 2 * chunksize; Commented Mar 31, 2014 at 11:20
  • Please pick one language. It seems you've chosen C++. C is not the same. Commented Mar 31, 2014 at 11:23

2 Answers 2

1
DWORD inputSize_part1 = inputSize / 3;
DWORD inputSize_part2 = inputSize / 3;
DWORD inputSize_part3 = inputSize - inputSize_part1 - inputSize_part2;

PBYTE inputData_part1 = inputData;
PBYTE inputData_part2 = inputData + inputSize_part1;
PBYTE inputData_part3 = inputData + inputSize_part1 + inputSize_part2;

Now you have three pointers and three sizes for the three chunks: the first two are a third or slightly less, and the third chunk may be slightly larger if the original was not evenly divisible. You'll need to delete[] inputData when you're doing using all of the chunks.

Sign up to request clarification or add additional context in comments.

Comments

0

The actual chunk size shouldn't matter. Following up on @pmg's comment, after the line input.read(reinterpret_cast<char*>(inputData), File_To_Read_Size);, you have a single array to store the data. The size of the shortest array will always be File_To_Read_Size / 3 because that is the number of arrays you are splitting it into. For access, the index used would be slightly augmented by the index of the chosen array, like so:

//assume array_index is the number of the array you are accessing (1,2, or 3)
//and position is the location you are trying to access in that array
BYTE result = inputData[position + (array_index - 1)];

Using this, all you need to know is what File_To_Read_Size % 3 is. If File_To_Read_Size % 3 == 1, then the first array contains the last BYTE. Else if File_To_Read_Size % 3 == 2, then the second array contains the last BYTE. Otherwise, all the arrays are the same size. Each of the first two ifs would thus be a check for whether the index is valid.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.