...
After the loop ends, if feof(stdin) != 0
, the loop has read through to the end of the file without encountering a newline character. Similarly, if ferror(stdin) != 0
, a read error occurred before the loop encountered a newline character, and if chars_read > index
, the input string has been truncated. void FIO34-C. Use int to capture the return value of character IO functions that might be used to check for end of file is also applied in this solution.
...
Code Block |
---|
while (((ch = getchar()) != '\n') && ch != EOF) |
See void FIO35-C. Use feof() and ferror() to detect end-of-file and file errors when sizeof(int) == sizeof(char) for the case where feof()
and ferror()
must be used instead.
...
Code Block | ||||
---|---|---|---|---|
| ||||
#define BUF_LENGTH 1024 #include <stdio.h> void get_data(void) { enum { BUF_LENGTH = 1024 }; char buf[BUF_LENGTH]; fscanf(stdin, "%s", buf); */ /* rest of function } |
...
Code Block | ||||
---|---|---|---|---|
| ||||
#define BUF_LENGTH 1024 #include <stdio.h> void get_data(void) { enum { BUF_LENGTH = 1024 }; char buf[BUF_LENGTH]; fscanf(stdin, "%1024s", buf); /* rest of function */ } |
...
...