配置文件
1,192.168.87.12
1,192.168.87.13
1,192.168.87.14
1,192.168.87.15
1,192.168.87.16
1,192.168.87.17
读代码
#include <stdio.h>
#include <vector>
#define IP_LEN 16
int init_ips(std::vector<char*> *pips,const char * path);
void destroy_ips(std::vector<char*> *pips);
#define TEST_READ_IP_LIST
#ifdef TEST_READ_IP_LIST
int main(){
std::vector<char*> ips;
const char *path="D:\\Work\\0823\\test\\IP.txt";
init_ips(&ips,path);
std::vector<char*>::iterator it;
for(it=ips.begin();it!=ips.end();it++){
printf("IP : %s\n",*it);
printf("IP : %s\n",strrchr(*it,'.')+1);
}
destroy_ips(&ips);
printf("Over\n");
getchar();
return 0;
}
#endif
int init_ips(std::vector<char*> *pips,const char * path){
char *ip;
int index;
FILE *fp=fopen(path,"rb");
fseek(fp,0,SEEK_SET);
while(!feof(fp)){
ip=(char*)malloc(IP_LEN);
memset(ip,0,IP_LEN);
fscanf(fp,"%d,%[^\n]",&index,ip);
if(strlen(ip)==0 && index>0){
printf("file is invalid.\n");
break;
}else{
pips->push_back(ip);
}
}
fclose(fp);
return pips->size();
}
void destroy_ips(std::vector<char*> *pips){
std::vector<char*>::iterator it;
for(it=pips->begin();it!=pips->end();){
if(*it!=0)
free(*it);
it=pips->erase(it);
}
}