开发者

Why am I getting an ferror on my source file using zlib deflate?

开发者 https://www.devze.com 2023-01-07 04:24 出处:网络
EDIT // open output file for writing if ( ( outfilefd = fopen( file_name, \"w+t\" ) ) == NULL ) { fprintf(stderr, \"Unable to create file\\n\");

EDIT

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

Write to the file, then need to zip it.

Opening a .z file and then calling 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 (r开发者_JAVA百科et != Z_OK)
            printf("ZLIB Error");

using def(), right from the 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 is not null, strm.avail_in = 16343, in has the data but ferror(source) returns 32?

EDIT - Also strm.avail_in = 16343 caught my eye as CHUNK = 16384....is that OK?

Any ideas or help is appreciated.

Thank You.


You should open the file in binary mode instead of text mode:

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

精彩评论

暂无评论...
验证码 换一张
取 消