From what I've read, the Linux terminal (in default settings) buffers the input and only sends it after receiving either EOF
or '\n'
.
When I loop c = getchar();
and check each c
for being EOF
(end then break) I need to do CTRL-D twice in order to stop reading, as the first EOF
is always consumed by the terminal (I know one can change the terminal to raw, but maybe this is a bit overkill).
However, when I check for c
being '\n'
(which also sends the input) it will not be consumed.
Example:
- Input: "abc\n"
- Characters read:
'a'
,'b'
,'c'
,'\n'
- Characters read:
- Input: "abc" and Ctrl-D
- Characters read:
'a'
,'b'
,'c'
- Characters read:
- Input: "abc", Ctrl-D and Ctrl-D again
- Characters read:
'a'
,'b'
,'c'
,EOF
- Characters read:
- Input: "abc", Return and Ctrl-D
- Characters read:
'a'
,'b'
,'c'
,'\n'
,EOF
- Characters read:
Isn't this highly inconsistent? Or is there any rationale behind it?
I want to parse input including whitespaces and thus cannot check for '\n'
but for EOF
- is that possible without changing the terminal to raw?
I've tried with feof(stdin)
too, but apperently this doesn't work either :/