aoc/2018/1/main.c

36 lines
571 B
C
Raw Normal View History

2023-04-17 18:18:20 +00:00
#include <stdio.h>
#include <stdlib.h>
int
main(int argc, char *argv[])
{
FILE *stream;
char *line = NULL;
size_t len = 0;
int nread;
if (argc != 2) {
fprintf(stderr, "Usage: %s <file>\n", argv[0]);
exit(EXIT_FAILURE);
}
stream = fopen(argv[1], "r");
if (stream == NULL) {
perror("fopen");
exit(EXIT_FAILURE);
}
int number;
int total = 0;
while ((nread = getline(&line, &len, stream)) != -1) {
number = atoi(line);
total += number;
}
printf("%d\n", total);
free(line);
fclose(stream);
exit(EXIT_SUCCESS);
}