char  tmp[20] = "123,124,125"的时候,怎样用函数取得"125"?char tmp1[5];
char tmp2[5];
char tmp3[5];sscanf(tmp,"%s,%s,%s",tmp1,tmp2,tmp3);但是tmp3不等于125,而是为空,
有什么函数能一下取得"125"吗?
谢谢!!!

解决方案 »

  1.   

    int cha_num(char *str,int begin = 0,int len = 3)//字符串,开始的索引,长度
    {
    char *p = new char[len];
    for(int i=begin,int j = 0;j<len;)
    p[j++] = str[i++];
    int temp = atoi(p);
    delete []p;
    return temp;
    }
      

  2.   

    ggglivw() 谢谢
    但我想用一个c语言函数实现哦/哪位有好?
      

  3.   

    晕,你不会把new用malloc,delete用free替换就可以了
      

  4.   

    int cha(char *str,int len)
    { int i,j,k,h,num = 0;
    for(i = 0;i<len;i++)
    {
    k=1;j = len-1;

    while(j>i)
    {
    k=k*10;
    j--;
    }
    h = str[i] - '0';
    num = num+h*k;
    }
    return num;
    }
    int cha_num(char *str,int begin = 0,int len = 3)//字符串,开始的索引,长度
    {
    char *p = new char[len];
    for(int i=begin,int j = 0;j<len;)
    p[j++] = str[i++];
    int temp = cha(p,len);
    delete []p;
    return temp;
    }
    malloc和free的问题自己解决
      

  5.   

    我想用单个的函数实现阿
    sscanf(tmp,"%s,%s,%s",tmp1,tmp2,tmp3);但是tmp3不等于125,而是为空,
    有什么函数能一下取得"125"吗?
    谢谢!!!
    sscanf(tmp,"%s,%s,%s",tmp1,tmp2,tmp3);
    类与这样的
      

  6.   

    char  tmp[20] = "123,124,125"的时候,怎样用函数取得"125"?char temp1[4] ;
    memcpy(temp1, temp[8], 3) ;
    temp[3] = '\0' ;
      

  7.   

    char tmp1[5];
    char tmp2[5];
    char tmp3[5];
    void cha_num(char *str,char *des,int begin = 0,int len = 3)//字符串,目的字符,开始的索引,长度
    {

    for(int i=begin,int j = 0;j<len;)
    des[j++] = str[i++];
    des[len] = '\0';

    }
    char  tmp[20] = "123,124,125"的时候,怎样用函数取得"125"?
    cha_num(tmp,tmp1,0,3);
    printf("%s\n",tmp1);