Q:

Sum of numbers contained in strings

Given a text file, for example:
cdcd dcdaqw cdcd KKAN 89 bxb hqhq1bhA
100uuz
cnkx

 n jk xjx100mc
...
.+1+

I want to find the sum of all  the numbers contained in the various strings:
In the given example, I want to sum 89 100 1 100 1.
I made many attempts at this(IE, fscanf of each string and trying to save the int values in an array), but they all failed.

A:

So this is fairly straightforward. At every position in the file we ask "is there a number here?". If there is, print it out. If there isn't try starting at the next character.
Based on how we understand fscanf() works, we'll test each character at most twice.
Code
#include <stdio.h>
#include <stdbool.h>

int main() {
    FILE * fd = fopen("in.txt", "r");

    while (true) {
        unsigned number;
        int rc = fscanf(fd, "%u", &number);
        if (rc == EOF)
            break;
        else if (rc == 0)
            fseek(fd, 1, SEEK_CUR);
        else if (rc == 1)
            printf("%u\n", number);
    }
}

Input File
cdcd dcdaqw cdcd KKAN 89 bxb hqhq1bhA
100uuz
cnkx

 n jk xjx100mc
...
.+1+

Output
89
1
100
100
1

Next steps
It should be fairly obvious how to extend this to report the sum, instead of the numbers themselves.

