Last active
May 19, 2020 00:20
-
-
Save cosinekitty/9ef2232f1458d10d987e2d2157456274 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| int main() | |
| { | |
| const size_t size = 1000; | |
| char *line; | |
| FILE *infile; | |
| double a, b, c, sum; | |
| line = malloc(size); | |
| if (line == NULL) | |
| { | |
| printf("Out of memory!\n"); | |
| return 1; | |
| } | |
| infile = fopen("input.txt", "rt"); | |
| if (infile == NULL) | |
| { | |
| printf("Cannot open input file.\n"); | |
| free(line); | |
| return 1; | |
| } | |
| sum = 0.0; | |
| while (fgets(line, size, infile)) | |
| { | |
| if (3 != sscanf(line, "%lf %lf %lf", &a, &b, &c)) | |
| { | |
| printf("BAD INPUT\n"); | |
| fclose(infile); | |
| free(line); | |
| return 1; | |
| } | |
| sum += a * b * c; | |
| } | |
| fclose(infile); | |
| free(line); | |
| printf("SUM=%lf\n", sum); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment