Linux安全网 - Linux操作系统_Linux 命令_Linux教程_Linux黑客

会员投稿 投稿指南 本期推荐:
搜索:
您的位置: Linux安全网 > Linux集群 > Architecture > » 正文

对硬盘文件进行二分查找

来源: chenbingchenbing 分享至:


uint32_t  binary_search_file(FILE *fpfile  ,int  m_termcount , int single_length,  uint64_t  termsign)
{
  int low=0,high=m_termcount-1;
  int mid;
 
  char  *ptemp=(char *)calloc ( single_length ,sizeof (char ));
  uint32_t  seekOffset=0;
  while (low<=high)
    {
      mid=(low+high)/2;  
      seekOffset=mid  *single_length;
      if(  fseek(fpfile,seekOffset,SEEK_SET)== 0)
{
 if(    fread(ptemp,single_length,1 , fpfile )==1)
   {
     if(  *( uint64_t *)ptemp ==  termsign )
{
 free (ptemp);
 return     seekOffset;
}
     else  if  (   *( uint64_t *)ptemp <  termsign )
{
 low=mid+1;
}
     else
{
 high=mid-1;
}
   }
 
}
    }
  free (ptemp);
  return -1;   
}

Tags:
分享至:
最新图文资讯
1 2 3 4 5 6
验证码:点击我更换图片 理智评论文明上网,拒绝恶意谩骂 用户名:
关于我们 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 发展历史