I am trying to parse a string in dynamic C. The string is the response of a valco valve to a position query. The string is usually of the form:
“2 Position is = xx”
I can split this string to only return the “xx” in C but not in dynamic C. Here is the code I am currently using:
#include
#include
int main ()
{
char actualpos[] =“2 Position is = 13”;
char * valco2pos;
valco2pos = strtok(actualpos,“=”);
valco2pos = strtok (NULL, “=”);
if (valco2pos[0] == ’ ')
memmove(valco2pos, valco2pos+1, strlen(valco2pos));
printf("%s
", valco2pos);
}
Any advice?
When I run that code with Dynamic C 10.72A, I see “13”. What happens when you run it? Did you run your tests with some other code or with an older version of Dynamic C?
Here’s a version that doesn’t modify the source string:
#include
#include
const char *parse_valco(const char *src)
{
src = strchr(src, '='); // find the equal sign
if (src != NULL) {
++src; // point past the equals sign
while (*src == ' ') {
++src; // skip over any spaces
}
}
return src;
}
int main ()
{
char actualpos[] = "2 Position is = 13";
const char *valco2pos;
valco2pos = parse_valco(actualpos);
if (valco2pos == NULL) {
printf("couldn't find equal sign
");
} else {
printf("%s
", valco2pos);
}
}