Chinaunix首页 | 论坛 | 博客
  • 博客访问: 91522
  • 博文数量: 16
  • 博客积分: 367
  • 博客等级: 一等列兵
  • 技术积分: 245
  • 用 户 组: 普通用户
  • 注册时间: 2011-10-25 22:37
文章分类

全部博文(16)

文章存档

2012年(4)

2011年(12)

我的朋友

分类: C/C++

2012-03-22 13:26:34

写了一个读配置文件的接口,要取出类似"192.168.0.43\t80\thttps\tabc\n"的字符串的配置信息

 

思路:以\t为标识来分割字符串,用两个指针分别指向一个字段的头尾,然后写入传进来的指针里面

 

void get_cond(char *cond, char *ip, int *port, char *proto, char *txt)

{

              char *p, *p1, tmp[1024]={0};

              p = cond;

              p1 = cond;

              int flag = 0;

             

              while(*p != '\n'){

                     if(*p == '\t'){

                            flag ;

                            if(flag == 1){

                                   for(; p1 - p < 0; p1 ){

                                          sprintf(tmp strlen(tmp), "%c", *p1);

                                   }

                                   printf("%s\n",tmp);

                                   ip = strdup(tmp);

                                   printf("ip:%s\n", ip);

                                   sprintf(tmp, "");

                                   p1 ;     

                            }else if(flag == 2){

                                   for(; p1 - p < 0; p1 ){

                                          sprintf(tmp strlen(tmp), "%c", *p1);

                                   }

                                   printf("%s\n",tmp);

                                   port = atoi(tmp);

                                   printf("port:%d\n", port);

                                   sprintf(tmp, "");

                                   p1 ;     

                            }else if(flag == 3){

                                   for(; p1 - p < 0; p1 ){

                                          sprintf(tmp strlen(tmp), "%c", *p1);

                                   }

                                   printf("%s\n",tmp);

                                   proto = strdup(tmp);

                                   printf("proto:%s\n", proto);

                                   sprintf(tmp, "");

                                   p1 ;     

                                  

                                  

                            }

                     }

                     p ;

              }

              if(flag == 3){

                     for(; p1 - p < 0; p1 ){

                                          sprintf(tmp strlen(tmp), "%c", *p1);

                     }

                     printf("%s\n",tmp);

                     txt = strdup(tmp);

                     printf("txt:%s\n",txt);

              }

             

}

 

int main()

{

       char *ip, *proto, *txt;

       int port;

       char cond[] = "192.168.0.43\t80\thttps\tabc\n";

      

       get_cond(cond, ip, &port, proto, txt);

             

       printf("========ip:%s\n",ip);

       printf("========port:%d\n",port);

       printf("========proto:%s\n",proto);

       printf("========txt:%s\n",txt);

}

 

结果奇怪的是:在接口里面打印出来的字段都是正常的,但是在main函数里面调用之后,字段全部取的不正确了:

 打印了一下字段的地址,发现地址也变了,不知道是什么原因,后来仔细分析,找了问题所在:

  在main函数里面,传给调用函数get_cond的指针是没有内存空间的,该空间要在接口函数里面分配,但是函数调用完毕之后,这些指针就被释放掉了,所以,如果想使传进去的指针有效果,可以传入该指针的地址,如此一来,在接口函数里面分配空间,也就等于给指针分配了空间了。具体一点说,函数就可接口以改成这样:void get_cond(char *cond, char **ip, int *port, char **proto, char **txt),里面具体代码就不贴了,有兴趣的朋友可以自己改好了试试,结果会如你所料的了。O(∩_∩)O~~

阅读(3248) | 评论(3) | 转发(0) |
给主人留下些什么吧!~~

sunny_piza2012-10-05 20:58:17

<6>Pid: 0, comm: swapper Tainted: P          NX 2.6.32.12-0.7-default #1 Thurley
<6>RIP: 0010:[<ffffffffa082a945>]  [<ffffffffa082a945>] __nf_conntrack_confirm+0x215/0x2d0 [nf_conntrack]
<6>RSP: 0018:ffff880048465ab0  EFLAGS: 00010202
<6>RAX: 00000000000058e5 RBX: ffff88013dcc0ba0 RCX: ffff88013dcc0bc0
<6>RDX: 0000000000000000 RSI: ffff880100135848 RDI: 000000000b7f7f7f

sunny_piza2012-10-05 20:57:43

netdev@vger.kernel.org