BartC said:Because we're doing line-oriented input.
You /can/ program at a character-level, but it would be like a syntax parser
looking at its input character-by-character instead of tokenising first,
then examining whole tokens.
You can at least consider the possibility of looking at more than one
character. Because it /is/ line-oriented, it means someone can enter Yoghurt
for yes, and Nigella for no, which is probably a bigger sin than
not checking for EOF on a supposedly interactive terminal.
Sure, you can consider the possibility of doing any kind of input you
If the program's behavior depends only on the first character on a line,
there's no point in storing the entire line. If you need to consider
the entire line, then sure, you probably need to store the entire line
-- which means you have to allow (in *some* way) for arbitrarily long
You need to decide how you want the program to behave before you can
decide how to implement that behavior.
I've been using fixed-length buffers for line-oriented text-files for
decades. If it doesn't work because of a line-length problem, then the file
isn't what I would call line-oriented. (There's a simple test: can you edit
the file without (a) having to scroll a hundred screens horizontally to see
the end of any line; and (b) can you see the line without it wrapping over a
hundred lines vertically. If not, then it isn't a line-oriented text file as
is normally understood, and needs to be processed differently; as a
character-oriented one, for example.)
So 99 screens would be ok?
As for being able to read any conceivable input, that's an unnecessary
complication. Either the file is simply and practically line-oriented, or
it's not, so it's an error.
Fine -- and then you have to decide how to deal with such an error, and
write the code to handle it.
Something doesn't sound right about asking the user to type Y or N (perhaps
in answer to something critical such as formatting a hard drive), and then
being tolerant about lines thousands of characters long, that just happen to
start with a Y!
Define the requirements, then we can talk about how to implement them.
If you want to check the first character and reject very long lines, you
still don't need to store the entire line, just keep a count.