我正在开发iOS项目
我必须以4KB的块读取一个巨大的文件 以下是我到目前为止的情况:
NSData *fileData= [self getBytesFromInput];
pj_str_t text;
int chunkSize = 4*1024;
int fileSize = [fileData length];
while (fileSize>0){
if (fileSize<=chunkSize) {
chunkSize = fileSize;
fileSize=0;
}
else fileSize = fileSize-chunkSize;
pj_strset(&text, (char*)[fileData bytes], MIN([fileData length], chunkSize); //takes the first chunk
//BUT HOW TO TAKE THE NEXT CHUNK OF DATA?
//do something with the &text ....
}
答案 0 :(得分:0)
我会重构代码,所以你也是以块的形式加载文件,但你可以通过向字节指针添加一个偏移量来访问后面的块:
int currentOffset = 0;
while (fileSize>0) {
...
char* bytePointer = (char*)[fileData bytes];
pj_strset(&text, bytePointer+currentOffset, MIN([fileData length], chunkSize);
currentOffset += chunkSize;
}
修改强>
这应该做同样的事情,但是读取块的文件块:
NSString *yourFilePath;
NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:yourFilePath];
int chunksize = 4*1024;
pj_str_t text;
NSData *data;
while ((data = [fileHandle readDataOfLength:chunksize]) && [data length] > 0) {
pj_strset(&text, (char*)[data bytes], [data length]);
}