Created
May 19, 2020 00:34
-
-
Save cosinekitty/72c08287d8966b5c4cc64ffa635886ab 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 = NULL; | |
| FILE *infile = NULL; | |
| int status = 1; /* assume failure unless everything succeeds */ | |
| double a, b, c, sum; | |
| line = malloc(size); | |
| if (line == NULL) | |
| { | |
| printf("Out of memory!\n"); | |
| goto fail; | |
| } | |
| infile = fopen("input.txt", "rt"); | |
| if (infile == NULL) | |
| { | |
| printf("Cannot open input file.\n"); | |
| goto fail; | |
| } | |
| sum = 0.0; | |
| while (fgets(line, size, infile)) | |
| { | |
| if (3 != sscanf(line, "%lf %lf %lf", &a, &b, &c)) | |
| { | |
| printf("BAD INPUT\n"); | |
| goto fail; | |
| } | |
| sum += a * b * c; | |
| } | |
| printf("SUM=%lf\n", sum); | |
| status = 0; | |
| fail: | |
| if (infile != NULL) fclose(infile); | |
| free(line); | |
| return status; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment