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

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 [email protected] fileName the filename to be read [email protected] size the initial size of the buffer [email protected] 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

This note was uploaded on 03/07/2010 for the course CS 6913 taught by Professor Torsensuel during the Spring '10 term at NYU Poly.

Page1 / 2

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

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