【问题标题】:I need to divide a large file into chunks.since my file size is greater(50GB) i need to split into larger chunks我需要将一个大文件分成块。因为我的文件大小更大(50GB)我需要分成更大的块
【发布时间】:2014-12-04 06:27:55
【问题描述】:
#include<iostream>
#include<fstream>
#define BUFFER_SIZE 11788889

using namespace std;

int main()
{
ifstream infile("hello.txt");
unsigned char buffer[BUFFER_SIZE];
int read_file_position=infile.tellg();
cout<<"input file position"<<read_file_position<<endl;
while(infile.read((char *)buffer,BUFFER_SIZE))
{
read_file_position=infile.tellg();
cout<<"input file position"<<read_file_position<<endl;

}
}

我尝试将我的文件仅拆分为字节块..拆分为 MB 或 GB 会很棒..如果有办法将它分成更大的块,那将很有帮助..因为我的记录没有固定长度,因此块大小会有所不同。

【问题讨论】:

  • 为什么需要把它分成块?您是指在内存中,还是在磁盘上将其拆分为单独的较小文件?
  • 因为我需要进一步提供那些特定的夹头来分离线程..
  • 但这是后半部分..首先分成块是主要的

标签: c++ buffer fstream chunks


【解决方案1】:

如果您想按块读取数据,而不是将块传递给多个线程,请执行以下操作

void *pManyChunks = malloc( NUM_THREADS * sizeof(YourRecord) );

while( not end of file )
{
  read sizeof(YourRecord)*NUM_THREADS bytes to pManuChunks

  pass (YourRecord*)((char*)pManuChunks + sizeof(YourThread)*0) pointer and sizeof(YourRecord) to first thread
  pass (YourRecord*)((char*)pManuChunks + sizeof(YourThread)*1) and sizeof(YourRecord) to second thread
  pass (YourRecord*)((char*)pManuChunks + sizeof(YourThread)*2) and sizeof(YourRecord) to third thread
  etc
}

【讨论】:

  • 我的记录大小不固定..它会有所不同...n与文件大小相同。我使用#定义缓冲区大小n文件大小,以便我可以对其进行任何更改给定的时间点..我想将我的文件分成块以便有效读取...一旦记录被正确拆分,线程就会发生。dt现在不是主要问题..只有拆分成正确的块才是。
【解决方案2】:

是的,但由于我有一个更大的文件,我不想将它写入另一个文件并浪费时间......

我有这样的记录..

ID:1002:: TP://reports/timing_report1.txt::TPS:counter/ffa::TPE: counter/ffd:: PGR: CLK::PTY:max::SL:-0.48::LAY:M2:: SEL::SLLT:1.0:: PTY:ANY::LAY:M1&M2:: PRG:ANY:: CELL:ANY:: REG:ANY
ID:1003:: TP://reports/timing_report1.txt::TPS:counter/ffb::TPE: counter/ffc:: PGR:CLK:: PTY:max::SL:-0.3::LAY:M1:: SEL::SLLT:1.0:: PTY:ANY::LAY: M1&M2:: PRG:ANY:: CELL:ANY:: REG:ANY

现在,如果我想分成块..我不希望一个块包含一半的记录..所以我想要一个块有完整的记录..如果我分成 2 个半部分,那么我不会' t 想要将一条记录分成两半。所以我需要搜索下一个 ID 的出现,并从该块中的下一个 ID 中添加上一个块 n 中的前半部分

【讨论】:

    猜你喜欢
    • 2017-09-18
    • 2010-11-19
    • 1970-01-01
    • 2017-08-25
    • 1970-01-01
    • 2023-03-24
    • 2018-12-24
    • 2015-12-03
    • 1970-01-01
    相关资源
    最近更新 更多