gzip-lilu

gzip-lilu - { char *buffer=(char *)malloc(size); int...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Hi, Prof. Suel, I have an alternative way to read gz file into a buffer by using gzread function. I think it is easier to understand. The implementation is as follows. #define INDEX_CHUNK 409600 //50KB #define DATA_CHUNK 20971520 //2.5MB char *memAlloc(gzFile *, int); int main (int argc, char * argv[]) { gzFile *cData,*cIndex; char *indexBuffer, *dataBuffer; cIndex=gzopen("nz2_merged/0_index","r"); cData=gzopen("nz2_merged/0_data","r"); indexBuffer=memAlloc(cIndex, INDEX_CHUNK); dataBuffer=memAlloc(cData, DATA_CHUNK); } /****************************************** *Read a gz file into a buffer *@param fileName the filename to be read *@param size the initial size of the buffer *@return the buffer ******************************************/ char *memAlloc(gzFile *fileName, int size)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: { char *buffer=(char *)malloc(size); int oldSize=size; int count=0; //The number of bytes that already read while (!gzeof(fileName)) { count+=gzread(fileName,buffer+count,oldSize); if (count==size) // Reallocate when buffer is full { oldSize=size; size*=2; buffer=(char *)realloc(buffer,size); } } return buffer; } The usage of gzread function is like fread, except there is no SEEK_END which means we cannot get file size then allocate buffer. gzread will read "oldSize" bytes data from "fileName" into address "buffer+count", and return the number of bytes already read. If there are remaining bytes in the file, we need to reallocate the buffer of double size and read the rest bytes. The implementation is correct after tests. Thanks, Li Lu...
View Full Document

Page1 / 2

gzip-lilu - { char *buffer=(char *)malloc(size); int...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online