Originally Posted by
Telume9
Hi,
I am very new to C and I'm difficulty reading two specific numbers in the same line from a text file.
For example, I have a txt file with lines that look like this:
line 1 :000002 0.6749 0.3434 0.87 0.8234 -0.0003 0.345 121.77 0.673956 0.256482
line 2: 000002 0.4738 0.1946 0.28 0.1659 -0.0002 0.562 103.67 0.194628 0.103829
The highlighted numbers at the end of each line are the ones I want to read and then use them in a function.
Those last two numbers are not uniquely marked in any way that makes them easy to discern from the rest, so read the whole line in with fscanf() to pull out all the numbers, and only use the last 2...
Code:
double num1, num2, total;
double scrap;
// open your file
fscanf(FILE, "%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf",&scrap,&scrap,&scrap,&scrap,&scrap,&scrap,&scrap,&scrap,&num1,&num2);
total = num1 + num2;