写了两天了,仍是没写出来
写了两天了,还是没写出来
学C已经两周了,感觉还是要动手写。写了两天手机号归属地查询,已经写了两天了各种问题,总是出不来结果,求各位帮帮新人
第一个是用的结构体的
内存地址相同,但是结果输出不正确
第二个是用数组的,完全就是走不通
求各位帮帮小弟
------解决思路----------------------
typedef struct PHONE{ char *phonenum; char *province; char *city; char *operators; char *code; }PHONE;
这些指针都没分配空间,指向的是局部变量char line[256] 中的内容。
当函数返回时,这个空间就可能被改写掉了
------解决思路----------------------
对于strncpy()函数,最常犯的一个错误是:认为无论如何目标位置上的字符串会以一个’\0′结尾,但实际情况不是这样的。strncpy(dst, src, n)的行为方式是:
(1)至多从src拷贝n个char;
(2)如果src中’\0’之前的字符数少于n,则’\0’将被作为填充字符写入dst直到写入dst的字符总数到达n;
(3)如果src中的字符数大于等于n,那么仅前n个字符将被拷贝;
(4)仅当src中的字符数少于n(不包括src中的’\0’)时dst中才会以’\0’结尾。
------------------
typedef struct PHONE{
char *phonenum;
char *province;
char *city;
char *operators;
char *code;
}PHONE;
要是嫌麻烦,可以将struct里面的指针都改写成数组。
------解决思路----------------------
PHONE *phone =NULL ;
typedef struct PHONE{
char phonenum[30];
char province[30];
char city[50];
char operators[30];
char code[30];
}PHONE;
phone->phonenum = phonenum; //strcpy(phone->phonenum,phonenum);
phone->province = province;
phone->city = city;
phone->operators = operators;
phone->code = code;
------解决思路----------------------
学C已经两周了,感觉还是要动手写。写了两天手机号归属地查询,已经写了两天了各种问题,总是出不来结果,求各位帮帮新人
第一个是用的结构体的
#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 PHONE{ char *phonenum; char *province; char *city; char *operators; char *code; }PHONE;
这些指针都没分配空间,指向的是局部变量char line[256] 中的内容。
当函数返回时,这个空间就可能被改写掉了
------解决思路----------------------
对于strncpy()函数,最常犯的一个错误是:认为无论如何目标位置上的字符串会以一个’\0′结尾,但实际情况不是这样的。strncpy(dst, src, n)的行为方式是:
(1)至多从src拷贝n个char;
(2)如果src中’\0’之前的字符数少于n,则’\0’将被作为填充字符写入dst直到写入dst的字符总数到达n;
(3)如果src中的字符数大于等于n,那么仅前n个字符将被拷贝;
(4)仅当src中的字符数少于n(不包括src中的’\0’)时dst中才会以’\0’结尾。
------------------
typedef struct PHONE{
char *phonenum;
char *province;
char *city;
char *operators;
char *code;
}PHONE;
要是嫌麻烦,可以将struct里面的指针都改写成数组。
------解决思路----------------------
PHONE *phone =NULL ;
typedef struct PHONE{
char phonenum[30];
char province[30];
char city[50];
char operators[30];
char code[30];
}PHONE;
phone->phonenum = phonenum; //strcpy(phone->phonenum,phonenum);
phone->province = province;
phone->city = city;
phone->operators = operators;
phone->code = code;
------解决思路----------------------
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;