gpt4 book ai didi

c++ - zlib gzread函数读取.gz文件时出错?

转载 作者:行者123 更新时间:2023-11-30 16:45:48 41 4
gpt4 key购买 nike

如标题,这是我的代码:

int decompress_one_file(char *infilename, char *outfilename)
{
gzFile infile = gzopen(infilename, "rb");
FILE *outfile = fopen(outfilename, "wb");
if (!infile || !outfile) return -1;

char buffer[128] = {NULL};
int num_read = 0;
num_read = gzread(infile, buffer, sizeof(buffer)); // crash here
while (num_read > 0) {
fwrite(buffer, 1, num_read, outfile);
}

gzclose(infile);
fclose(outfile);
return 0;
}

当我的控制台应用程序运行到gzread()时,它崩溃了,我不知道这是什么类型的错误?。 zlib版本:1.2.11

更新压缩功能:

int compress_one_file(char *infilename, char *outfilename)
{
FILE *infile = fopen(infilename, "rb");
gzFile outfile = gzopen(outfilename, "wb");
if (!infile || !outfile) return -1;

char inbuffer[128] = {NULL};
int num_read = 0;
unsigned long total_read = 0, total_wrote = 0;
while ((num_read = fread(inbuffer, 1, sizeof(inbuffer), infile)) > 0) {
total_read += num_read;
gzwrite(outfile, inbuffer, num_read);
}
fclose(infile);
gzclose(outfile);
return 0;
}

显示错误:

error windows

有人知道这个错误吗?

最佳答案

我检查过,它在我的工作站上运行良好。可能是 gzip 压缩文件有问题?你能显示一个导致崩溃的文件吗?

但无论如何,你都会有一个 fwrite 的无限循环:

int num_read = 0;
while ((num_read = gzread(infile, buffer, sizeof(buffer))) > 0) {
while (num_read > 0) {
num_read -= fwrite(buffer, 1, num_read, outfile);
}
}

关于c++ - zlib gzread函数读取.gz文件时出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43974322/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com