readln() returns new line charater
Stewart Gordon
smjg_1998 at yahoo.com
Tue Dec 31 08:34:44 PST 2013
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.
More information about the Digitalmars-d
mailing list