在mswin32上成功使用gzdopen后,gzread失败了

时间:2014-03-10 20:20:59

标签: zlib libz

在zlib 1.2.8的windows版本中似乎存在一些问题。根据{{​​3}}中的USAGE.txt(来自zlib.net)以及gzread和gzdopen的手册页,代码应该可以工作。有人可以开导我吗?顺便说一句,我已经在darwin(使用zlib 1.2.5)和linux(使用zlib 1.2.1)上测试了这段代码,其中gzdopen可以工作。

以下是代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include "zlib.h"

int main(int argc, char *argv[]) {
        char            buffer[500];
        int             len;
        gzFile          gin;
        const char      *errmsg;
        int             errnum;
        char            *fun      = argv[1];
        char            *filename = argv[2];
        printf("fun: %s filename: %s\n", fun, filename);
        printf("zlib version: %s\n", zlibVersion());
        if (!strcmp(fun, "gzdopen")) {
                int fd = open(filename, O_RDONLY);
                printf("open returned %d\n", fd);
                gin = gzdopen(fd, "rb");
        }
        if (!strcmp(fun, "gzopen"))
                gin = gzopen(filename, "rb");
        printf("gzopen returned %p\n", gin);
        len = gzread(gin, buffer, sizeof(buffer));
        fprintf(stderr, "gzread() returned %d\n", len);
        errmsg = gzerror(gin, &errnum);
        fprintf(stderr, "gzerror() returned '%s'; errnum=%d\n", errmsg, errnum);
        return 0;
}

这就是我的所作所为:

C:\decompress> cl test.c /Iinclude lib/zdll.lib
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8804 for 80x86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.

test.c
Microsoft (R) Incremental Linker Version 6.00.8447
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

C:\decompress> gzip -c < test.c > test.c.gz

C:\decompress>test gzdopen test.c.gz
fun: gzdopen filename: test.c.gz
zlib version: 1.2.8
open returned 3
gzopen returned 00332CE0
gzread() returned -1
gzerror() returned '<fd:3>: Bad file descriptor'; errnum=-1

C:\decompress>test gzopen test.c.gz
fun: gzopen filename: test.c.gz
zlib version: 1.2.8
gzopen returned 00332CC0
gzread() returned 500
gzerror() returned ''; errnum=0

0 个答案:

没有答案