Just when you see <start>
then scan 3 numbers into double. You have the line content in line
variable, you can use strtod to scan string into double. You can even use sscanf(line, "<start>%lf %lf %lf</start>", &x, &y, &z);
, but using strtod is better for error handling.
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stddef.h>
#include <stdlib.h>
#define MAX_LINE_LENGTH 1024
void print_lines(FILE *stream) {
double a, b, c;
char line[MAX_LINE_LENGTH];
while (fgets(line, MAX_LINE_LENGTH, stream) != NULL) {
char *pnt;
// locate substring `<start>` in the line
if ((pnt = strstr(line, "<start>") != NULL)) {
// advance pnt to point to the characters after the `<start>`
pnt = &pnt[sizeof("<start>") - 1];
char *pntend;
// scan first number
a = strtod(pnt, &pntend);
if (pnt == pntend) {
fprintf(stderr, "Error converting a value.\n");
// well, handle error some better way than ignoring.
}
pnt = pntend;
// scan second number
b = strtod(pnt, &pntend);
if (pnt == pntend) {
fprintf(stderr, "Error converting a value.\n");
// well, handle error some better way than ignoring.
}
pnt = pntend;
// scan third number
c = strtod(pnt, &pntend);
if (pnt == pntend) {
fprintf(stderr, "Error converting a value.\n");
// well, handle error some better way than ignoring.
}
printf("Read values are %lf %lf %lf\n", a, b, c);
} else {
// normal line
//fputs(line, stdout);
}
}
}
int main()
{
print_lines(stdin);
return 0;
}