2010-07-16 51 views
1

编辑为什么我在使用zlib deflate的源文件中获得一个恐怖?

// open output file for writing 
    if ((outfilefd = fopen(file_name, "w+t")) == NULL) 
    { 
     fprintf(stderr, "Unable to create file\n"); 
     exit(1); 
    } 

写入文件,那么就需要压缩它。

打开.Z文件,然后调用DEF()

FILE *zipFile; 

    if ((zipFile = fopen("C:\\LOGS\\test.txt.z", "w+t")) == NULL) 
    { 
     fprintf(stderr, "Unable to create file\n"); 
     exit(1); 
    } 



    int ret = def(outfilefd, zipFile, Z_DEFAULT_COMPRESSION); 
     if (ret != Z_OK) 
      printf("ZLIB Error"); 

使用DEF(),从一site

int def(FILE *source, FILE *dest, int level) 
    { 
     int ret, flush; 
     unsigned have; 
     z_stream strm; 
     unsigned char in[CHUNK]; 
     unsigned char out[CHUNK]; 

     /* allocate deflate state */ 
     strm.zalloc = Z_NULL; 
     strm.zfree = Z_NULL; 
     strm.opaque = Z_NULL; 
     ret = deflateInit(&strm, level); 
     if (ret != Z_OK) 
      return ret; 

     /* compress until end of file */ 
     do { 

      strm.avail_in = fread(in, 1, CHUNK, source); 
     int g = ferror(source);//<---------------- EROR HERE returning 32? 
      if (ferror(source)) { 
       (void)deflateEnd(&strm); 
       return Z_ERRNO; 
      } 

zipFile不为空,strm.avail_in = 16343,in已数据但ferror(source)返回32?

编辑 - 也strm.avail_in = 16343引起了我的CHUNK = 16384眼....行吗?

任何想法或帮助表示赞赏。

谢谢。

+1

我不知道是什么32。尝试[perror](http://www.opengroup.org/onlinepubs/000095399/functions/perror.html)以打印错误文本。 – 2010-07-16 01:03:23

+0

在您的平台上errno 32的含义是什么,您可能会误解(“”)吗?在Linux上它说破管,这听起来很奇怪,因为你打开一个文件。 – mvds 2010-07-16 01:05:18

+0

也许只是展示你如何打开源代码,这是你的问题,为什么我们应该假设或猜测? – mvds 2010-07-16 01:06:16

回答

2

你应该以二进制模式,而不是文本模式打开文件:

zipFile = fopen("C:\\LOGS\\test.txt.z", "w+b") 
+0

谢谢,我也尝试过,同样的错误和变量的状态... – 2010-07-16 00:55:12

+1

你的变量名称有点混乱,'outfilefd'实际上是输入文件 - 你打开了什么模式? – nos 2010-07-16 01:08:10

+0

“w + t”...试过“b”没有运气 – 2010-07-16 16:35:50

相关问题