readln with buffer fails

Justin Whear via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Oct 29 16:28:07 PDT 2014


On Wed, 29 Oct 2014 23:10:10 +0000, dcrepid wrote:

> On Wednesday, 29 October 2014 at 21:19:25 UTC, Peter Alexander wrote:
>> You need to take a slice of the buffer:
>>
>> char[] buf = Input[];
>> readln(buf);
>> // line now in buf
>>
>> The reason for this is because you need to know where the string ends.
>> If you just passed in Input, how would you know how long the line read
>> was?
> 
> Thanks, that solves the problem.  I guess what confuses me is that Input
> isn't a slice, or at least not implicitly convertible to one.
> 
> Also, I've tried using Input[] directly at the callsite but apparently
> that would be an rValue, and D doesn't do rValues yet.

Part of what readln does is *modify* the slice itself, not just the 
pointed-to
characters. In particular it alters the length member so that you know 
how much
input was actually read.  This is also why the rvalue reference shouldn't 
work.
Remember, D chose not to repeat C's mistake of relying on null 
terminators.


More information about the Digitalmars-d-learn mailing list