#include #include int main(int argc, char *argv[]) { if (argc < 2 || argc >4) { printf("Usage: ./avg \n"); exit(0); } char *filename = argv[1]; int numberOfLinse = -1; int startLine = 0; if (argc >= 3) numberOfLinse = atoi(argv[2]); if (argc == 4) startLine = atoi(argv[3]); printf("file is %s, startline is %d and linecount is %d \n", filename, numberOfLinse, startLine); long total = 0; int lines = 0; FILE *fp = fopen(filename, "r"); if (fp == NULL) { printf("unable to open the file \n"); exit(0); } char string[256]; int i = 0; if (startLine != 0) { printf("moving the starting position by the specified lines \n"); for (i = 0; i < startLine; i++) { if (fgets(string, 256, fp) == NULL) { printf("startline way too long \n"); exit(0); } } } if (numberOfLinse == -1) numberOfLinse = 100000; for(lines = 0; lines < numberOfLinse; lines++) { if (fgets(string, 256, fp) == NULL) break; total += (long)strtod(string, NULL); } printf("Lines = %d, Total = %ld Avg = %ld \n", lines, total, total/lines); }