Skip to content

Instantly share code, notes, and snippets.

@cosinekitty
Last active May 19, 2020 00:20
Show Gist options
  • Select an option

  • Save cosinekitty/9ef2232f1458d10d987e2d2157456274 to your computer and use it in GitHub Desktop.

Select an option

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