I have a file called test.txt the file contains:
<this is a test = 1>
<more tests = 42 and more "34">
<start>10.213123 41.21231 23.15323</start>
<random stuff = "4">
<blah 234>
When I see <start>
I want to scan in the 3 numbers after into a double like this:
x = 10.213123
y = 41.21231
z = 23.15323
I'm sort of confused because here, fgets scans the entire line, how can I scan in the 3 numbers into a double? Because the numbers can be of various lengths? I made this to print out what it reads from a file but I can't wrap my head around it.
void print_lines(FILE *stream) {
char line[MAX_LINE_LENGTH];
while (fgets(line, MAX_LINE_LENGTH, stream) != NULL) {
fputs(line, stdout);
}
}
Just when you see
<start>
then scan 3 numbers into double. You have the line content inline
variable, you can use strtod to scan string into double. You can even usesscanf(line, "<start>%lf %lf %lf</start>", &x, &y, &z);
, but using strtod is better for error handling.