mirror of
https://gitlab.com/80486DX2-66/gists
synced 2024-12-27 05:40:17 +05:30
freadln.*: handle EOF in a special way to indicate EOF
This commit is contained in:
parent
3638c9eb7c
commit
53d0b8908d
@ -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,
|
||||
|
@ -16,6 +16,7 @@ typedef size_t freadln_length_type;
|
||||
|
||||
enum freadln_status {
|
||||
freadln_OK,
|
||||
freadln_EOF,
|
||||
freadln_ERROR
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user