On 28/12/2013 16:49, Jeroen Bollen wrote:
Why is when you do readln() the newline character (\n) gets read too?
Wouldn't it make more sense for that character to be stripped off?

The newline character needs to be read - how else will it know when it's got to the end of the line? :)

Of course, that doesn't mean that it needs to be included in the string returned by readln. Indeed, this is an inconsistency - writeln adds a newline so, in order to match, readln ought to strip the newline away.

But sometimes you might want the newline. Maybe you're building up a string in memory from several lines, or you want to know whether the file ends with a newline or not. Indeed, there are three possibilities:

- you don't care about the newlines themselves, only the strings they delimit
- you care about the presence or absence of a final newline
- you want to preserve the distinction between different styles of newline (CR, LF, CRLF, whatever else).

Maybe readln should have an optional parameter so that you have the choice.

Stewart.

Reply via email to