拆分数组中的C / C ++块 [英] Split array into chunks in C/C++

查看:139
本文介绍了拆分数组中的C / C ++块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要寻找一种方式来拆分数组中的C / C多个阵列++,与标准的Win32和C / C ++的功能。

下面是一个例子,我是多么文件读入数组中。

 使用命名空间std;LPSTR File_To_Read =的file.exe;
DWORD File_To_Read_Size = NULL;
DWORD inputSize = NULL;
PBYTE inputData = NULL;ifstream的输入(File_To_Read,ifstream的二进制:: | :: ifstream的ATE);
File_To_Read_Size =的static_cast&所述; DWORD>(input.tellg());
input.seekg(input.beg);inputData =新的字节[File_To_Read_Size]
input.read(reinter pret_cast<字符*>(inputData),File_To_Read_Size);
input.close();

现在我要分割inputData这样。

  DWORD inputSize_part1;
DWORD inputSize_part2;
DWORD inputSize_part3;PBYTE inputData_part1;
PBYTE inputData_part2;
PBYTE inputData_part3;

因此​​,在以后我也可以把他们重新走到一起。

我应该如何进行?我会显示一个例子code什么,我都试过了,但我的code不会使没有太大意义的各位专家。

编辑:
@IKH块大小应该是大约相同的尺寸。因此,如果inputData是33KB,则inputData_part1(和inputSize_part1)应11KB,inputData_part2(和inputSize_part2)应11KB,等等。那么,到底会有3倍11KB阵列和DWORDS对它们的大小。


解决方案

  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;

现在您有三个指针和三个尺寸为三个部分:前两个是第三或略少,而第三组块可稍大如果原始不整除。你需要删除[] inputData 当你使用所有的块做的。

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.

解决方案

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.

这篇关于拆分数组中的C / C ++块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆