如何从C中的backtrace()输出中提取字符串并将其存储在char *中

时间:2016-10-06 21:42:41

标签: c

我得到了下面的回溯输出

/home/jalv3/adv/adv2/local/libprog.so(func+0x2d) [0x8fe46911ac6d]

现在我想只提取“libprog.so”和“0x8fe46911ac6d”并将其存储在char *中

尝试使用如图所示的strtok和strchr,但没有获得所需的输出

 char * str = "/home/jalv3/adv/adv2/local/libprog.so(func+0x2d) [0x8fe46911ac6d]";
    char * pp=strchr(str, '[');
    char line[256];
    char *subString;
    strcpy(line, str);
    subString = strtok(line,"/");
    subString=strtok(NULL,"(");
    printf("string 1= %s\n",pp);
    printf("string 2 = %s\n",subString);

输出

string 1 = [0x8fe46911ac6d] 
string 2 = jalv3/adv/adv2/local/libprog.so

1 个答案:

答案 0 :(得分:-1)

只需继续解析字符串即可获得所需的结果。例如,在最后printf之前添加以下代码以获得所需的结果。

/* subString = jalv3/adv/adv2/local/libprog.so at this point */

/* Reverse search to find the last '/' character */
subString = strrchr(subString, '/');

/* Jump past the '/' */
subString++

/* This will print: string 2 = libprog.so */
printf("string 2 = %s\n", subString);

我会留下另一根弦作为练习。