什么是错用下面的code [英] what is wrong with the following code

查看:326
本文介绍了什么是错用下面的code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的函数从 rabin_polynomial 结构文件偏移,打开 INPUT_FILE 作为MD5指纹的产生和结果写入 fpfile

The following function gets file offsets from the rabin_polynomial structure, opens the input_file for md5 fingerprint generation and writes the result to fpfile

我的问题是它似乎使用相同的 chunk_buffer 内容有些时候,它生成具有不同legths块类似指纹。

My problem is it seems to use the same chunk_buffer content some times that it generates similar fingerprints for chunks with different legths.

这可能是什么原因?

我已分别测试了MD5函数与其他输入和它产生正确的摘要。

I have tested the md5 function with other inputs separately and it generates correct digests.

   int write_rabin_fingerprints_to_binary_file(FILE *fpfile,FILE *input_file,
    struct rabin_polynomial *head)
{        
    struct rabin_polynomial *poly=head;
    unsigned char fing_print[33]={'\0'};
    size_t bytes_read;
      while(poly != NULL)
        {    
       char *chunk_buffer;
       chunk_buffer = (char*) malloc ((poly->length));  
       bytes_read=fread (chunk_buffer,1, poly->length,input_file);          
               if(bytes_read!=poly->length)
                {
                   printf("Error reading from%s ",input_file);
                   return -1;
                }
       strncpy((char*)fing_print,md5(chunk_buffer).c_str(),32);     
       size_t ret_val=fprintf(fpfile, "%llu\t%lu\t%s\n",poly->start,
                                       poly->length,fing_print);

              if(ret_val == 0)
               {
                  fprintf(stderr, "Could not write rabin polynomials to file.");
                  return -1;
               }

         poly=poly->next_polynomial;
      free(chunk_buffer);
        }

   return 0;
}

编辑:

我使用Visual Studio 2010中能值映射为的char * 的malloc()行创建问题运行这个程序?

I am running this program using visual studio 2010. Could typecasting to char * in the malloc() line create the problem?

读的字节数是一样在参数指定。

The number of bytes read is just as specified in the argument.

推荐答案

有没有什么错在code会导致此类故障。我只是发现它的发生是因为零长度字符串这也被称为文件孔

There was nothing wrong in the code to cause such faults. I just found out that it happened because of zero-length strings which are also called as file holes.

这篇关于什么是错用下面的code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆