官方给的链接打不开,而且里面的逻辑,每次都会打开文件,所以自己做了点个修改,发上来,借大家参考:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#include <fstream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <assert.h> class IP17MON {
public:
static bool init(const std::string strPathToDataFile = "./17monipdb.dat") {
if(is_init) {
return true;
} printf("Opening %s\n", strPathToDataFile.c_str());
std::ifstream ifDataFile(strPathToDataFile.c_str(), std::ios::binary);
if (ifDataFile.is_open() == false) {
printf("%m\n");
return false;
}
// std vector reserve
vecDataFile.assign(std::istreambuf_iterator<char>(ifDataFile), std::istreambuf_iterator<char>());
printf("Load %lu bytes success\n", vecDataFile.size()); unsigned int uiIndexLen = 0;
memcpy(&uiIndexLen, &vecDataFile[0], 4);
uiIndexLen = ntohl(uiIndexLen);
printf("uiIndexLen = %d\n", uiIndexLen);
pIPIndex = &vecDataFile[4];
pIPData = &vecDataFile[uiIndexLen];
is_init = 1;
return true;
} static std::string find(const std::string strIP) {
struct sockaddr_in stSockAddrInet; memset(&stSockAddrInet, 0, sizeof(struct sockaddr_in));
if(inet_aton(strIP.c_str(), &stSockAddrInet.sin_addr) == 0) {
printf("convert error\n");
return "";
}
unsigned int uiIP = ntohl(stSockAddrInet.sin_addr.s_addr); // atoi The string can contain additional characters after those
// that form the integral number, which are ignored and have no
// effect on the behavior of this function.
int iFirst = atoi(strIP.c_str());
int iStart = 0;
memcpy(&iStart, pIPIndex+(iFirst*4), 4);
printf("iStart = %d\n", iStart);
int iMaxComLen = pIPData - pIPIndex - 1024 - 4; int iIndexOffset = -1;
unsigned char ucIndexLength = 0;
for (iStart = iStart * 8 + 1024; iStart < iMaxComLen; iStart += 8) {
unsigned int uiCurrIP = 0;
memcpy(&uiCurrIP, pIPIndex+iStart, 4);
uiCurrIP = ntohl(uiCurrIP);
if (uiCurrIP >= uiIP) {
iIndexOffset = 0;
memcpy(&iIndexOffset, pIPIndex+iStart+4, 3);
memcpy(&ucIndexLength, pIPIndex+iStart+7, 1);
break;
}
} if (iIndexOffset == -1) {
return "";
} std::string strRegion(pIPData + iIndexOffset - 1024, ucIndexLength);
return strRegion;
}
public:
static std::vector<char> vecDataFile;
static char *pIPIndex, *pIPData;
static int is_init;
};
char *IP17MON::pIPIndex;
char *IP17MON::pIPData;
int IP17MON::is_init = 0;
std::vector<char> IP17MON::vecDataFile; extern "C" const char *find_monip(const char *ip)
{
if (IP17MON::init() == false) {
return "";
}
return IP17MON::find(ip).c_str();
}

  

C语言解析17monipdb.dat(http://www.ipip.net/)免费数据库的更多相关文章

  1. atitit.java解析sql语言解析器解释器的实现

    atitit.java解析sql语言解析器解释器的实现 1. 解析sql的本质:实现一个4gl dsl编程语言的编译器 1 2. 解析sql的主要的流程,词法分析,而后进行语法分析,语义分析,构建sq ...

  2. C语言解析WAV音频文件

    C语言解析WAV音频文件 代码地址: Github : https://github.com/CasterWx/c-wave-master 目录 前言 了解WAV音频文件 什么是二进制文件 WAV的二 ...

  3. 纯真IP数据库(qqwry.dat)转换成最新的IP数据库格式(ipwry.dat)

    纯真IP数据库(qqwry.dat)转换成最新的IP数据库格式(ipwry.dat) 转载自:http://blog.cafeboy.org/2011/02/25/qqwry-to-ipwry/ ht ...

  4. VBA中使用JavaScript脚本语言解析JSON数据

    JSON:JavaScript 对象表示法(JavaScript Object Notation) 和xml相似,都是文本形式(保存在文本文件中或字符串等形式),比如: jsstr = {" ...

  5. cJSON_json包的C语言解析库

    cJSON库描述 CJSON是一个用于解析JSON包的C语言库,库文件为cJSON.c和cJSON.h, 所有的实现都在这两个文件中.原作者的地址cJSON. JSON包的解析 例如有一个JSON的数 ...

  6. NOIP2014-提高组初赛C语言解析(选择填空题)

    第二十届全国青少年信息学奥林匹克联赛初赛 一.单项选择题(共 20 题,每题 1.5 分,共计 30 分.每题有且仅有一个正确选项) 1. 以下哪个是面向对象的高级语言( B ) A.汇编语言   B ...

  7. 使用go语言解析xml

    操作系统: CentOS 6.9_x64 go语言版本: 1.8.3 问题描述 现有一个自动报障程序,如果服务出错会自动给指定人发送邮件,配置文件内容如下(default.xml): <?xml ...

  8. go语言解析网页利器goquery使用教程(爬虫必备)

    某些时候需要爬取网页中指定信息时,通常需要一些框架解析网页行成dom模型,然后来操作节点来获取相应的信息.在java中很显然就是Jsoup,而在Golang里,应该就是这个goquery了吧. goq ...

  9. 书籍推荐《以C语言解析电脑》

    这本书要想买到,在大陆看起来比较难,理出个目录,看个大概: 另外在这个地方可以预览前20页:http://openebook.hyread.com.tw/ebookservice/hyviewer/o ...

随机推荐

  1. css常用居中

    对一个已知大小的元素上下左右居中(已知大小了,直接margin也就行了): css如下:.parent{height:100px;width:100px;background:grey;positio ...

  2. Kubernetes使用cephfs作为后端存储

    这里使用了k8s自身的持久化卷存储机制:PV和PVC.各组件之间的关系参考下图: PV的Access Mode(访问模式) The access modes are: ReadWriteOnce – ...

  3. 【C语言】模拟实现printf函数(可变参数)

    一.printf函数介绍 printf功能 printf函数是格式化输出函数,一般用于向标准输出设备按规定格式输出信息. printf原型 int printf( const char* format ...

  4. POJ1185炮兵阵地【动态规划】

    炮兵阵地 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 26892   Accepted: 10396 Descriptio ...

  5. reactjs点滴记录

    reactjs: render方法后面,}后面,不要加分号,加逗号,因为是对属性赋值,否则报错: var Test = React.createClass({ render:function(){re ...

  6. Myeclipse8.5开发-插件安装二:安装findbugs方法

    环境:Myeclipse8.5 step 1:首先从官网下载findbugs插件:http://downloads.sourceforge.net/project/findbugs/findbugs% ...

  7. 从零开始用 Flask 搭建一个网站(三)

    从零开始用 Flask 搭建一个网站(二) 介绍了有关于数据库的运用,接下来我们在完善一下数据在前端以及前端到后端之间的交互.本节涉及到前端,因此也会讲解一下 jinja2 模板.jQuery.aja ...

  8. [Oracle]审计Audit

    1.Audit的概念 Audit是监视和记录用户对数据库进行的操作,以供DBA进行问题分析.利用Audit功能,可以完成以下任务: 监视和收集特定数据库活动的数据.例如管理员能够审计哪些表被更新,在某 ...

  9. 本地yum服务搭建

    1.准备linux ISO系统镜像文件 (例如:rhel-server-5.5-i386-dvd.iso) 2.linux虚拟机(centos 7  192.168.50.24 ),启动sshd服务 ...

  10. js字符串的操作

    js中字符串的使用非常普遍,以下是一些常用的方法和属性,字符串以str='abcdabc'举例. 1.length属性 获取字符串的长度,str.length返回7 2.replace()方法 str ...