sephora sephora - 4 months ago 30
C Question

How to extract string from backtrace() ouput in C and store it in char*

I got the backtrace output as below

/home/jalv3/adv/adv2/local/ [0x8fe46911ac6d]

Now I want to extract only "" and "0x8fe46911ac6d" and store it in a char *

Tried to use strtok and strchr as shown , but not getting the desired output

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

The output is

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


Simply continue parsing the string to get the result you want. For example, add the following code before the last printf to achieve the result you want.

/* subString = jalv3/adv/adv2/local/ at this point */

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

/* Jump past the '/' */

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

I will leave the other string as an exercise for you.