2010-07-16 52 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

相關問題