Hello.
I am trying to make parser for string. string contains double number so I am using atof() to make double from it. but when I try it makes something strange when use strcopy() to make char* from string. Is it my error anywhere?
double r = 0, vel = 0; int paramindex_begin, paramindex_end; string substring; for(paramindex_begin = command.size(); settings_string[paramindex_begin-1] != 'r'; paramindex_begin++); for(paramindex_end = paramindex_begin; paramindex_end < settings_string.size() && settings_string[paramindex_end] != ' ' && settings_string[paramindex_end] != '\n'; paramindex_end++); substring = settings_string.substr(paramindex_begin, paramindex_end-paramindex_begin); strcpy(settings_char, substring.c_str()); r = atof(settings_char);
after this I stop by breakpoint and see: substring = "380" settings_char = 0xFF r = 0
Thanks.
The interesting thing when there is a strange problem, is to dump the variable contents of all variables involved.
So the input string your for loop operates on the values used when extracting a sub-string, the contents you get in the C++ string variable. And the contents of the C string after strcpy(), ... so you get the full sequence of events from what you think is a valid input string and all the way to your unexpected output data. This normally directly identifies where the code deviates from your assumptions.
In this case, you haven't even posted the full data type information for all variables. And can we be completely sure that you have correctly working dynamic memory for the C++ string operations?