#include #include #include "common.h" const size_t BUF_SIZE = 1024; static void read_line(char *buf) { if (fgets(buf, BUF_SIZE, stdin) == NULL) { if (ferror(stdin)) die("I/O error reading line"); if (feof(stdin)) { buf[0] = '\0'; /* Unclear if fgets does this already */ return; } die("Unexpected error reading line"); } } static void write_line(const char *line) { FILE *f = fopen(FILENAME, "a"); if (f == NULL) die_err("Error opening output file"); if (fputs(line, f) == EOF) die("Error writing to output file"); if (fclose(f) != 0) die_err("Error closing output file"); } int main() { char buf[BUF_SIZE]; for (read_line(buf); buf[0]; read_line(buf)) { write_line(buf); } return 0; }