so I’m using the zlib package on Ubuntu. I’m trying to figure out how to use gzopen and gzread correctly, this is what I have so far
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <zlib.h>
#define NUM_BUFFERS 8
#define BUFFER_LENGTH 1024
char buf[BUFFER_LENGTH];
int main(int argc, const char* argv[])
{
int status;
gzFile file;
file = gzopen("beowulf.txt", "w");
int counter = 0; /*when the counter reachers BUFFERS_FULL, stop*/
if(file == NULL)
{
printf("COULD NOT OPEN FILE\n");
return 1;
}
while(counter < NUM_BUFFERS)
{
status = gzread(file, buf, BUFFER_LENGTH - 2);
printf("STATUS: %d\n", status);
buf[BUFFER_LENGTH - 1] = "\0";
printf("%s\n", buf);
counter++;
}
gzclose(file);
printf("STATUS: %d\n", status);
return 0;
}
The gzread("STATUS: %d\n",status); returns -2, and I have no clue why. Any help would be appreciated.
Mode
"w"indicates that you are preparing to create a new archive:You’ve just truncated the file to zero length.
Also, you should use the binary mode flag:
"wb"or"rb".Also, it’s a bit weird that your supposed .gz-archive has an extension
.txt.Read the docs, docs rule. 🙂