直接上代碼(哪一位有不明白的地方,請留言):

//#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

//#define _DEBUG_

long read4Bit(ifstream& f);
long read3Bit(ifstream& f);
int  readString(ifstream& f, long exeOffset, char *str);
int  read2Zero(ifstream& f, char*buf);
int findIPLocation(char*dat, long exeOffset, DWORD searchIpNum);
void printHelp(char* exePath);
DWORD convertIP(char*sip);
void printIpNum(DWORD ip);

int main(int argc, char**argv)...{
    char *sip;
    char *dat;  long offSet;

    DWORD intIP;
#if defined _DEBUG_
    //for(int i=0;i<argc; i++){
    //    cout<<"argv["<<i<<"/"<<argc<<"]="<<argv[i]<<" ";
    //}
#endif
    dat=new char[256];
    memset(dat, 0, 256);
    if( argc==3 )...{
        strcpy(dat, argv[2]);
        offSet=0L;
    }else...{
        delete[]dat;
        printHelp(argv[0]);
        return 1;
    }

    sip=new char[16];
    memset(sip, 0, 16);
    strcpy(sip, argv[1]);
    intIP = convertIP(sip);
    findIPLocation(dat, offSet, intIP);

    delete[]dat;    delete[]sip;

    return 0;
}

//第一個參數 是數據文件
//第二個參數 是如果合併成一個文件的話,開始的偏移
//第三個參數 是查找的IP的十進制表達
//第四個參數 是返回的結果
int findIPLocation(char*dat, long exeOffset,DWORD searchIpNum) ...{
#if defined _DEBUG_
    int times=0;  //查找了多少次
#endif

    DWORD ipBeginPos, ipEndPos; //開始的偏移,結束的偏移
    DWORD midNum, beginNum, endNum, totalNum;  //中間的個數,開始的個數,結束的個數,總共的個數
    DWORD ip1Num, ip2Num;       //開始IP的十進制值,結束IP的十進制值
    long pos, iRead;                //當前的文件的指針的位置
    char *ipAddr; //*ipAddr2, *ipAddr1, *ipAddr;

    if(searchIpNum<0xFF)...{
        return -1;
    }

    ifstream fs;
    fs.open(dat, ios::binary | ios::in, 0);

    if((!fs.is_open()) || (!fs.good())) ...{
        return -2;
    }

#if defined _DEBUG_
    cout<<" searchIpNum="<<searchIpNum<<", exeOffset="<<exeOffset<<endl;
#endif
    fs.seekg(exeOffset, ios_base::beg);
    ipBeginPos=read4Bit(fs);
#if defined _DEBUG_
    cout<<" ipBeginPos="<<ipBeginPos<<endl;
#endif
    ipEndPos=read4Bit(fs);
#if defined _DEBUG_
    cout<<" ipEndPos="<<ipEndPos<<endl;
#endif
    totalNum = (ipEndPos - ipBeginPos) / 7;
#if defined _DEBUG_
    cout<<" totalNum="<<totalNum<<endl;
#endif
    if(totalNum<1)...{
        return -3;
    }

    beginNum = 0;
    endNum   = totalNum;
    while( 1 ) ...{ //IP的索引是有順序的,按照個數來二分查找.
        midNum= beginNum+(DWORD)((endNum - beginNum)/2);
        pos = ipBeginPos+midNum*7;
        fs.seekg(exeOffset+pos, ios_base::beg);
#if defined _DEBUG_
        cout<<"times="<<++times<<", beginNum="<<beginNum<<", endNum="<<endNum
            <<", midNum="<<midNum<<", exeOffset+pos="<<exeOffset+pos
            <<endl;
#endif
        ip1Num=read4Bit(fs);
        if( ip1Num<1 ) ...{
            fs.close(); cout<<"[1]while(){...}"<<endl;
            return -2;
        }
#if defined _DEBUG_
        //printIpNum(ip1Num);
#endif
        fs.seekg(3, ios_base::cur);
        ip2Num=read4Bit(fs);
        if( ip2Num<1 ) ...{
            fs.close(); cout<<"[3]while(){...}"<<endl;
            return -2;
        }
#if defined _DEBUG_
        //printIpNum(ip2Num);
#endif

        if( (ip1Num<=searchIpNum) && (searchIpNum<=ip2Num) )...{
            fs.seekg(exeOffset+pos, ios_base::beg);
            ip1Num=read4Bit(fs);
#if defined _DEBUG_
            cout<<"  Last position:"<<endl<<endl;
            printIpNum(ip1Num);
            cout<<"pos="<<pos<<"   ";
#endif
            pos=read3Bit(fs);
#if defined _DEBUG_
            cout<<"pos="<<pos<<endl;
#endif
            fs.seekg(exeOffset+pos, ios_base::beg);
            ip2Num=read4Bit(fs);
#if defined _DEBUG_
            printIpNum(ip2Num);
#endif
            break;
        }else if(ip1Num>searchIpNum)...{
            endNum = midNum;
        }else if(ip2Num<searchIpNum)...{
            beginNum = midNum;
        }
    }

#if defined _DEBUG_
    cout<<endl<<"Found position:"<<fs.tellg()<<endl;
#endif

    ipAddr=new char[512];
    memset(ipAddr, 0, 512);
    iRead = readString(fs, exeOffset, ipAddr);
    if(iRead>0)...{
        cout<<ipAddr<<"  ";
    }

    memset(ipAddr, 0, 512);
    iRead = readString(fs, exeOffset, ipAddr);
    if(iRead>0)...{
        cout<<ipAddr;
    }

    delete[]ipAddr;

    fs.close();
    return 0;

}

long read4Bit(ifstream& f)...{
    long t; char c[4];

    t=0;
    f.read((char*)&t, 4);

    return t;
}

long read3Bit(ifstream& f)...{
    long t;
    f.read((char*)&t, 3);
    return t;
}

int readString(ifstream& f, long exeOffset, char * str)...{
    char ipFlag; long ipSeek, tmpSeek;
    int ret;
    ipFlag=f.get();
    if(ipFlag==0x1) ...{ // 重定向模式1: 城市信息隨國家信息定向
        ipSeek=read3Bit(f);
        f.seekg(exeOffset+ipSeek, ios_base::beg);
        ret=readString(f, exeOffset, str);
    }else if(ipFlag == 0x2) ...{ // 重定向模式2: 城市信息沒有隨國家信息定向
        ipSeek=read3Bit(f);
        tmpSeek=f.tellg();
        f.seekg(exeOffset+ipSeek, ios_base::beg);
        ret=readString(f, exeOffset, str);
        f.seekg(exeOffset+tmpSeek, ios_base::beg);
    } else ...{
        f.seekg(-1, ios_base::cur);
        ret=read2Zero(f, str);
    }
    return ret;
}

int read2Zero(ifstream& f, char*buf)...{
    f.get(buf, 128, 0);
    return strlen(buf);
}


void printHelp(char* exePath)...{
    char *fileName;
    fileName=strrchr(exePath,'/');
    if(fileName==NULL)...{
        strcpy(fileName, exePath);
    }else...{
        fileName++;
    }
    cout<<endl
        <<"Wry IP Search Beta by cangwu.lee@gmail.com "
        <<"Usage:"
        <<" "<<fileName<<"<ip address> <Path to wry.dat> "
        <<endl;
    free(fileName);
}


DWORD convertIP(char*sip)...{

    char *c1, *c2, *c3, *c4;
    DWORD i1, i2, i3, i4, dd;

#if defined _DEBUG_
    cout<<" ip="<<sip<<endl;
#endif

    c1 = strtok(sip, " ,.-");
    if(c1==NULL)...{
        cout<<"IP address incorrect."<<endl;
        return 0;
    }
    c2 = strtok(NULL," ,.-");if(c2==NULL)c2="0";
    c3 = strtok(NULL," ,.-");if(c3==NULL)c3="0";
    c4 = strtok(NULL," ,.-");if(c4==NULL)c4="0";

    i1=atol(c1); i2=atol(c2); i3=atol(c3); i4=atol(c4);

    if(i1<0 || i2<0 || i3<0 || i4<0
        || i1>254 || i2>255 || i3>255 || i4>255)...{
        cout<<"IP address incorrect."<<endl;
        delete[]c1;      delete[]c2;     delete[]c3;     delete[]c4;
        return 0;
    }

    delete[]c1;    delete[]c2;    delete[]c3;    delete[]c4;

    dd =(i1<<24) + (i2<<16) + (i3<<8) + i4;
    return dd;
}

void printIpNum(DWORD ip)...{
    long ip1, ip2, ip3, ip4, dd1, dd2, dd3;
    char addr[16];
    ip1=(ip>>24) & 0xFF;           dd1=ip1<<24; // 16777216;
    ip2=( (ip-dd1)>>16) & 0xFF;    dd2=ip2<<16; //(ip-dd1) / 65536;
    ip3=( (ip-dd1-dd2)>>8) & 0xFF; dd3=ip3<<8;  //(ip-dd1-dd2) / 256;
    ip4=ip-dd1-dd2-dd3;

    memset(addr, 0, 16);
    sprintf(addr,"%3.3d.%3.3d.%3.3d.%3.3d", ip1, ip2, ip3, ip4);
    addr[15]=0;
    cout<<" ip="<<ip<<"   addr="<<addr<<endl;
    delete[]addr;
}

測試完就貼上來的,代碼可能有點亂。寫得不好的地方,請指正。

C++查詢wry.dat中的IP地址信息相关推荐

  1. python使用正则表达式抽取文件中的IP地址

    python使用正则表达式抽取文件中的IP地址 IP正则 r'(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' 有效IP ((25[0-5]|2[0-4][0-9]|[01] ...

  2. Python通过snmp获取交换机VLAN号、VLAN默认网关、VLAN子网掩码和ARP表中的IP地址与MAC对应记录数据

    自己做项目时,自己封装的Python通过snmp获取交换机VLAN号.VLAN默认网关.VLAN子网掩码和ARP表中的IP地址与MAC对应记录数据. myPySnmp.py源代码 "&quo ...

  3. php 什么函数获取ip,在PHP中获取ip地址的方法有哪些

    在PHP中获取ip地址的方法有哪些 发布时间:2021-02-15 18:37:36 来源:亿速云 阅读:97 作者:Leah 这篇文章给大家介绍在PHP中获取ip地址的方法有哪些,内容非常详细,感兴 ...

  4. ubuntu18的网关ip在哪里配_技术|如何在 Ubuntu 18.04 LTS 中配置 IP 地址

    在 Ubuntu 18.04 LTS 中配置 IP 地址的方法和以往使用的配置方法有很大的不同.和旧版本的不同之处在于,Ubuntu 18.04 使用 Netplan 来配置 IP 地址,Netpla ...

  5. 使用 Python 从文件中提取 IP 地址

    让我们看看如何使用 Python 从文件中提取 IP 地址. 算法 : 为正则表达式导入 re 模块. 使用 open() 函数打开文件. 读取文件中的所有行并将它们存储在列表中. 声明 IP 地址的 ...

  6. 运用awk提取日志文件中的IP地址

    在我们的日常工作中,可能会遇到各种不同的问题,有来自内部,有的来自外部. 以Nginx服务为例,服务上线之后可能会遭受非法的流量攻击,我们在配置Nginx服务时可以通过Nginx.conf配置文件中的 ...

  7. 字符串中查找IP地址的正则表达式

    字符串中查找IP地址的正则表达式: IP地址:(2[0-4]\d|25[0-5]|[01]\d\d|\d\d|\d)\.(2[0-4]\d|25[0-5]|[01]\d\d|\d\d|\d)\.(2[ ...

  8. 如何centos7中查看IP地址

    如何centos7中查看IP地址 1.查看centos7的IP地址 毫无疑问,首先是启动并进入centos7 输入ip查询命名 ip addr ,会出现如下图所示的情况:centos的ip地址是ens ...

  9. 如何在 Ubuntu 18.04 LTS 中配置 IP 地址?

    在 Ubuntu 18.04 LTS 上配置 IP 地址的方法与旧方法有很大不同,与以前的版本不同,Ubuntu 18.04 使用Netplan实用程序,它是一个新的命令行网络配置实用程序,用于配置 ...

  10. 字符串双引号表示c语言,c语言中单引号和双引号的区别(顺利解决从字符串中提取IP地址的困惑)...

    c语言中的单引号和双引号可是有很大区别的,使用之前一定要了解他们之间到底有什么不同,下面小编就给大家详细的介绍一下吧,对此还不是很了解的朋友可以过来参考下 问题:从c++文件中将std:string转 ...

最新文章

  1. 成果展示 | 清华大学大数据研究中心——医工结合科研创新支持计划
  2. PS怎么将自己的形状存储为自定形状?
  3. Apache2 httpd.conf 配置详解(一)
  4. 21世纪初最有影响力的20篇计算机视觉期刊论文
  5. MongoDB只查询一个字段
  6. 禁用Zuul的过滤器
  7. 层与特征融合_【计算机系统应用】(第122期)感受野特征增强的 SSD 目标检测算法...
  8. 【DP】【树状数组】方伯伯的玉米田/优美玉米(luogu 3287/金牌导航 数据结构优化DP-5)
  9. PopupWindow和AlertDialog区别
  10. MNIST机器学习入门(二)
  11. java过滤集合数量,java – 使用lambdaj过滤集合
  12. [概率论与数理统计-1]: 总体架构、知识结构、知识体系
  13. 标准c语言与嵌入式,嵌入式C语言与C语言的区别
  14. 2020年408真题_2020年港澳台联考真题——化学!
  15. BottomNavigationView动态添加MenuItem
  16. 2020年Java集合课堂笔记
  17. SLF4J 简单日志门面 介绍和使用
  18. JBoot 全网上最好用的 API 文档工具
  19. C语言:实现蛇形数组
  20. 什么!FPS难道不是越高越好吗?

热门文章

  1. 三位数除以两位数竖式计算没有余数_三位数除以两位数有余数竖式运算300题
  2. Quadratic probing
  3. Chrome浏览器取色器
  4. 校园网内实现文件共享
  5. C/C++快速读写磁盘数据的方法
  6. 微信小程序之登录注册界面的实现
  7. 超立方体(n方体)Qn:递推式 和 性质
  8. No symbols loaded
  9. AtCoder题解 —— AtCoder Beginner Contest 184 —— E - Third Avenue - BFS
  10. 湿化学清洗过程中晶片污染控制方法