diff --git a/c-programming/io/freadln.c b/c-programming/io/freadln.c index 23de41d..79e98cb 100644 --- a/c-programming/io/freadln.c +++ b/c-programming/io/freadln.c @@ -10,7 +10,6 @@ * TODO: Figure out potential problems * TODO: Add 'flushing' of STDIN (while there are characters, read them) before * the input reading loop to avoid input queues - * [optional] TODO: Handle EOF in a special way to indicate EOF */ #include "freadln.h" @@ -22,6 +21,7 @@ int freadln(FILE* f, char** output, size_t* length_out) { * return value: * freadln_OK: no errors, the length of STDIN line has been stored in * `length_out` + * freadln_EOF: end of file * freadln_ERROR: an error occurred (see errno) * >= 0: length of read line */ @@ -71,6 +71,8 @@ int freadln(FILE* f, char** output, size_t* length_out) { errno = 0; freadln_epilogue; + if (character == EOF) + return freadln_EOF; return freadln_OK; } @@ -101,11 +103,13 @@ int main(void) { f = new_f; for (int i = 0; i < 4; i++) { size_t line_length; - if (freadln(f, &line, &line_length) != freadln_OK) { + int result = freadln(f, &line, &line_length); + if (result == freadln_ERROR) { perror("freadln"); exit(EXIT_FAILURE); - } else if (feof(f)) { - printf("File: EOF, breaking the loop\n"); + } else if (result == freadln_EOF || feof(f)) { + printf("File: EOF, breaking the loop (returned by function? %d, " + "feof? %d)\n", result == freadln_EOF, !!feof(f)); break; } printf("File, line #%d: '%s' (%zu characters)\n", i + 1, line, diff --git a/c-programming/io/freadln.h b/c-programming/io/freadln.h index 75af727..c98809e 100644 --- a/c-programming/io/freadln.h +++ b/c-programming/io/freadln.h @@ -16,6 +16,7 @@ typedef size_t freadln_length_type; enum freadln_status { freadln_OK, + freadln_EOF, freadln_ERROR };