我有一个 C++ 中的向量,我想将它写入 .bin 文件。这个向量的类型是byte
,字节数可能很大,可能是数百万。我这样做是这样的:
if (depthQueue.empty())
return;
FILE* pFiledep;
pFiledep = fopen("depth.bin", "wb");
if (pFiledep == NULL)
return;
byte* depthbuff = (byte*) malloc(depthQueue.size() * 320 * 240 * sizeof(byte));
if(depthbuff)
{
for(int m = 0; m < depthQueue.size(); m++)
{
byte b = depthQueue[m];
depthbuff[m] = b;
}
fwrite(depthbuff, sizeof(byte),
depthQueue.size() * 320 * 240 * sizeof(byte), pFiledep);
fclose(pFiledep);
free(depthbuff);
}
depthQueue
是我的向量,它包含字节,可以说它的大小是 100,000。
有时我没有收到此错误,但 bin 文件为空。
有时我得到堆错误。
有时当我调试它时,malloc 似乎没有分配空间。问题出在空间上吗?
还是顺序内存块太长而无法写入bin?