33,311
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct PHONE{
char *phonenum;
char *province;
char *city;
char *operators;
char *code;
}PHONE;
PHONE *readFile( char *phonenum);
PHONE *readFile( char *phonenum){
//截取手机号前三位用来定位手机段
char three[4] ;
strncpy( three,phonenum,3 );
//打开手机号段文件
FILE * fp;
fp = fopen(three, "r");
if (fp == NULL){
exit(EXIT_FAILURE);
}
char temp[8] ;
strncpy( temp,phonenum,7);
PHONE *phone =NULL ;
int linecount = 0;
while( !feof(fp) ){//文件未结束
char line[256] ;
fgets(line,256,fp);
//截取第一个"|"为手机号段
char *phonesection = strtok( line, "|");
//截取第二个"|"为归属地
char *area = strtok( NULL,"|");
//截取第三个"|"为运营商
char *operators = strtok(NULL,"|");
//截取第四个"|"为城市编号
char *code = strtok(NULL,"|");
//省份
char *province = strtok( area,",");
//城市
char *city = strtok( NULL,",");
if(strncmp( temp ,phonesection,7 ) == 0 ){
phone = (PHONE *)malloc( sizeof (PHONE) );
phone->phonenum = phonenum;
phone->province = province;
phone->city = city;
phone->operators = operators;
phone->code = code;
printf("%p,%p,%p,%p,%p\n", phone->phonenum,phone->province,phone->city,phone->operators,phone->code);
printf("%s,%s,%s,%s,%s\n", phone->phonenum,phone->province,phone->city,phone->operators,phone->code);
// printf("%p\n", phone);
fclose( fp );
return phone;
}
linecount++;
}
fclose( fp );
return phone;
}
int main(int argc, char const *argv[]){
char *phonenum = "13933007354";
PHONE *phone = readFile( phonenum);
printf("%p,%p,%p,%p,%p\n", phone->phonenum,phone->province,phone->city,phone->operators,phone->code);
printf("%s,%s,%s,%s,%s\n", phone->phonenum,phone->province,phone->city,phone->operators,phone->code);
// printf("%p\n", phone);
free( phone );
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char **readFile( char *phonenum);
char **readFile( char *phonenum){
//截取手机号前三位用来定位手机段
char three[4];
strncpy( three,phonenum,3 );
//打开手机号段文件
FILE * fp;
fp = fopen(three, "r");
if (fp == NULL){
exit(EXIT_FAILURE);
}
char temp[8] ;
strncpy( temp,phonenum,7);
char *result[5];
while( !feof(fp) ){//文件未结束
char line[256] ;
fgets(line,256,fp);
//截取第一个"|"为手机号段
char *phonesection = strtok( line, "|");
//截取第二个"|"为归属地
char *area = strtok( NULL,"|");
//截取第三个"|"为运营商
char *operators = strtok(NULL,"|");
//截取第四个"|"为城市编号
char *code = strtok(NULL,"|");
//省份
char *province = strtok( area,",");
//城市
char *city = strtok( NULL,",");
if(strncmp( temp ,phonesection,7 ) == 0 ){
result[0] = phonenum;
result[1] = province;
result[2] = city;
result[3] = operators;
result[4] = code;
printf("%s,%s,%s,%s,%s\n", result[0],result[1],result[2],result[3],result[4]);
fclose( fp );
printf("%p\n", result);
return result;
}
}
fclose( fp );
return result;
}
int main(int argc, char const *argv[]){
char *phonenum = "13933007354";
char **result;
result = readFile( phonenum );
printf("%p\n", result);
for( int i =0;i<5;i++){
printf("%s\n", result[i]);
}
return 0;
}
typedef struct {
char phonenum [30];
char province [30];
char city [50];
char operators[30];
char code [30];
} PHONE;
PHONE ph,*phone;
phone=&ph;
strncpy(phone->phonenum , phonenum ,30-1);phone->phonenum [30-1]=0;
strncpy(phone->province , province ,30-1);phone->province [30-1]=0;
strncpy(phone->city , city ,50-1);phone->city [50-1]=0;
strncpy(phone->operators , operators,30-1);phone->operators[30-1]=0;
strncpy(phone->code , code ,30-1);phone->code [30-1]=0;
typedef struct {
char phonenum [30];
char province [30];
char city [50];
char operators[30];
char code [30];
} PHONE;
PHONE ph,*phone;
phone=&ph;
strncpy(phone->phonenum , phonenum ,30);phone->phonenum [30-1]=0;
strncpy(phone->province , province ,30);phone->province [30-1]=0;
strncpy(phone->city , city ,50);phone->city [50-1]=0;
strncpy(phone->operators , operators,30);phone->operators[30-1]=0;
strncpy(phone->code , code ,30);phone->code [30-1]=0;