Not with scanf
, but also a possibility:
You could use strtok
to separate the wanted values and atof
to convert them to double. Be aware that unlike strtod
(which is a bit more complicated to use), atof
does not offer error handling. The values can be extracted using the following function, for example:
#include <string.h> // For strtok
#include <stdlib.h> // For atof
void scanValues(char* input, double* value1, double* value2) {
char* sep;
sep = strtok(input, "\""); // replaces the first \" with \0
sep = strtok(NULL, "\""); // replaces the second \" with \0,
*value1 = atof(sep); // sep points to the first wanted value
// Same for the second value:
sep = strtok(NULL, "\"");
sep = strtok(NULL, "\"");
*value2 = atof(sep);
}
It could be called as follows:
#include <stdio.h> // For printf
int main() {
double value1, value2;
char input[] = "\r\nOK\r\n\r\n+RSRP: 164,6200,\"-090.20\",\r\r\n+RSRQ: 164,6200,\"-07.30\",\r\n\r\nOK\r\n";
scanValues(input, &value1, &value2);
printf("%f %f\n", value1, value2);
}