Skip to content

Instantly share code, notes, and snippets.

@cosinekitty
Created May 19, 2020 00:34
Show Gist options
  • Select an option

  • Save cosinekitty/72c08287d8966b5c4cc64ffa635886ab to your computer and use it in GitHub Desktop.

Select an option

Save cosinekitty/72c08287d8966b5c4cc64ffa635886ab to your computer and use it in GitHub Desktop.
#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