功能:手机号段拆分
haoduan_to_vac_ftp.c
#include <stdio.h> #include <stdlib.h> #include <string.h> #define ROW 500L #define COLUMN 100L void find_district(char a[ROW][COLUMN],int j,char *province_value,char *district_value,char *district) { char *p; char tmp[100]; int line,mark=0; for(line=0;line<j;line++) { if(strstr(a[line],district)) { mark=0; memset(tmp,0,sizeof(tmp)); strcpy(tmp,a[line]); for(p=strtok(tmp,"\t");p;p=strtok(NULL,"\t")) { mark++; if(mark==1) strcpy(province_value,p); if(mark==3) { strcpy(district_value,p); break; } } } if(mark==3) break; } if(mark==0) { strcpy(province_value,"no found"); strcpy(district_value,"no found"); } else if(mark>1 && mark<3) { strcpy(district_value,"no found"); } return; } int main(int argc,char *argv[]) { FILE *fp; int i=0,line; int find_ret; char buf[1024]; char provinceanddistrict_array[ROW][COLUMN]; char province_value[64]; char district_value[64]; char *district; char *p,district_str[1024]; char tmp[64],str[64],strtok_tmp[200]; if(argc!=2) { fprintf(stderr,"Input: %s filename\n",argv[0]); exit(-1); } fp=fopen("provinceanddistrict-utf8.txt","r"); if(fp==NULL) { fprintf(stderr,"fopen() error.\n"); exit(-1); } while(fgets(provinceanddistrict_array[i++],1023,fp)!=NULL); //将省份号段读入数组 // printf("main i=%d\n",i); // line=--i; fp=fopen(argv[1],"r"); //打开号段文件 if(fp==NULL) { fprintf(stderr,"fopen() error.\n"); exit(-1); } while(fgets(district_str,1023,fp)!=NULL) { //每次读取一行号段文件,然后与上面数组中号段匹配 memset(buf,0,sizeof(buf)); memset(tmp,0,sizeof(tmp)); memcpy(buf,district_str,3); strcat(buf,","); memcpy(tmp,district_str+3,4); strcat(buf,tmp); /* strcpy(strtok_tmp,district_str); for(p=strtok(strtok_tmp,"\t");p;p=strtok(NULL,"\t")) district=p; */ for(p=strtok(district_str,"\t\n");p;p=strtok(NULL,"\t\n")) district=p; // printf("main district=%s\n",district); find_district(provinceanddistrict_array,i-1,province_value,district_value,district); strcat(buf,","); strcat(buf,province_value); strcat(buf,","); strcat(buf,district_value); printf("buf=%s\n",buf); } fclose(fp); exit(0); }