#include <stdio.h>

void average(FILE *fp, char *fname) {
    double x, sum = 0.0;
    int l = 0, n = 0, m, c;

    printf("\nFile %s:\n", fname);
    while ((m = fscanf(fp, "%lf", &x)) != EOF) {
        ++l;
        if (m == 1) {
            sum += x;
            ++n; 
            continue; /* ¼ÌÐøÑ­»·¶ÁÈë */
        }
        fprintf(stderr, "Date error. Line %d: ", l);
        while (!isspace(c = getc(fp)))
            fputc(c, stderr);
        fputc('\n', stderr);
    }

    printf("Average: %16.8f\n", sum / n);
}

/*
int main(void) {
    char name[256];
    FILE *fp;

    while (1) {
        printf("File name (end-of-file to end): ");
        if (scanf("%255s", name) == EOF) break;
        if ((fp = fopen(name, "r")) == NULL)
            fprintf(stderr, "Can't open file: %s\n", name);
        else {
            average(fp, name);
            fclose(fp);
        }
    }

    printf("Bye!\n");

    return 0;
}
*/

int main(int argc, char** argv) {
    FILE *fp;

    while (*++argv != NULL) {
        if ((fp = fopen(*argv, "r")) == NULL)
            printf("Can't open file: %s\n", *argv);
        else {
            average(fp, *argv);
            fclose(fp);
        }
    }

    printf("Bye!\n");

    return 0;
}


