Reputation: 58
I am trying to read a gz file into a char buffer which should resize depending on how big the file is.
gzFile infile = gzopen(infile_name, "rb");
char *buf = malloc(256);
int total_read = 0;
int incr_read = 0;
do {
incr_read = gzread(infile + total_read, buf, 256);
total_read += incr_read;
buf = realloc(buf, total_read + 256);
} while (incr_read > 0);
This stops immediately for some reason, only reading the first 256 bytes. There is no way it reached the end of the file, because it is 1.3 kB in size (compressed). How could I continue reading the rest of the file?
I am relatively new to C so there is likely something obvious I overlooked, but I have not been able to find a solution elsewhere.
Upvotes: 0
Views: 213
Reputation: 58427
The gzFile
is an opaque pointer to a struct that contains a bunch of internal state information, so you shouldn't do any arithmetic on it. The first argument to gzread
should be the same for every iteration of the loop.
Upvotes: 0
Reputation: 58
I was shifting the file pointer instead of the buffer pointer, which was why it wasn't working. it should have been gzread(infile, buf + total_read, 256);
Upvotes: 1
Reputation: 7287
The first parameter to gzread()
should be a gzFile
handle but you pass infile + total_read
for some reason instead of just infile
.
Upvotes: 1