Question: I have the following in a text file called: values.txt

```
1 4 
2.5 3.76
122 10
277.543 
165.4432

```

I am trying to read the content of this text file, and add each two pairs together and output the result ...
the output would be something like this :

```
1 Pair:(1, 4) = 5

2 Pair:(2.5, 3.76)= 6.26

```

and so on ..

I am opening the file like this 

```
int c;
FILE myfile; 

myfile= fopen("values.txt", "r"); 
if ( myfile == NULL ) { 
  printf("Cannot open TEXT file\n");
  return 1; 
}

double aa,bb;
while ( (c = getc(myfile) ) != EOF ) { 
    // HERE SHOULD I DO THE OUTPUT BUT HOW?
}

```

Any help is really appreciated ..

Language = C

Answer: 
The following code does what you expect. myfile should be declared as FILE\*. fopen returns a pointer to FILE structure. If the file is very large, I would recommend reading in buffers of big size (eg: 65535 etc) and parse it char by char and convert it to float values. It reduces system call overhead which takes more time than processing text to float values.

```
#include <stdio.h>
#include <string.h>

main(int argc, char *argv[])
{
    FILE* myfile; 

    myfile = fopen("values.txt", "r"); 
    if ( myfile == NULL ) { 
        printf("Cannot open TEXT file\n");
        return 1; 
    }

    double aa,bb;
    while (2 == fscanf(myfile, "%lf %lf", &aa, &bb)) {
        printf("%lf\n", aa+bb);
    }
    return 0;
}

```
You haven't shown what you need as output for the single-value lines, but this looks like a case for `fgets()` and `sscanf()`, unless you really want the two lines with a single value to be processed as a unit.

```
char buffer[256];
int rownum = 0;
while (fgets(buffer, sizeof(buffer), myfile) != 0)
{
    double aa, bb;
    int n = sscanf(buffer, "%lf %lf", &aa, &bb);
    if (n == 2)
        printf("%d Pair:(%g, %g) = %g\n", ++rownum, aa, bb, aa+bb);
    else if (n == 1)
        printf("%d Solo:(%g) = %g\n", ++rownum, aa, aa);
    else
    {
        printf("Failed to find any numbers in <<%s>>\n", buffer);
    }
}

```

If you used `fscanf(myfile, "%g %g", &aa, &bb)`, then it would read over newlines (they count as white space) looking for numbers, so it would read one number from one line, and the second from another line. This is not usually what people are after (but when it is what you need, it is extremely useful). Error recovery with `fscanf()` tends to be more fraught than with `fgets()` and `sscanf()`.