写了两天了,还是没写出来

蟲蟲O_O 2014-11-18 11:38:24
学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;
}


求各位帮帮小弟
...全文
102 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
熊熊大叔 2014-11-18
  • 打赏
  • 举报
回复
typedef struct PHONE{ char *phonenum; char *province; char *city; char *operators; char *code; }PHONE; 这些指针都没分配空间,指向的是局部变量char line[256] 中的内容。 当函数返回时,这个空间就可能被改写掉了
赵4老师 2014-11-18
  • 打赏
  • 举报
回复
纠正楼上:
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;
蟲蟲O_O 2014-11-18
  • 打赏
  • 举报
回复
明白了 局部变量所指向的地址在函数结束后会释放掉内存,这时指向这些局部变量的指针会找不到对应的值 所以phone->phonenum是有值的,而其他却没有值 非常感谢各位
赵4老师 2014-11-18
  • 打赏
  • 举报
回复
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;
赵4老师 2014-11-18
  • 打赏
  • 举报
回复
strncpy(dst, src, n);dst[n]=0;
xionggch 2014-11-18
  • 打赏
  • 举报
回复
PHONE *phone =NULL ;
引用 1 楼 truelance 的回复:
typedef struct PHONE{ char *phonenum; char *province; char *city; char *operators; char *code; }PHONE; 这些指针都没分配空间,指向的是局部变量char line[256] 中的内容。 当函数返回时,这个空间就可能被改写掉了
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;
忘世麒麟 2014-11-18
  • 打赏
  • 举报
回复
对于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里面的指针都改写成数组。
w74839520 2014-11-18
  • 打赏
  • 举报
回复
学c两周水平就这么好了啊。

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧