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. Let's say I wanted to write a program that would read and then add together the last two numbers of each line. So it would add the two red numbers together and then it would add the two green numbers together.
I know how to use fgets to read multiple numbers if each line in the file is just one number, but that's all I know how to do. My program so far is:
Code:#include <stdio.h> FILE *file; int main() { float limit; char line[50]; file = fopen("nfile.txt", "r"); while (fgets(line,20,file)!=NULL) { sscanf(line,"%f", &limit); printf("%.0f\n", limit); } fclose(file); return 0; }



LinkBack URL
About LinkBacks



