readln with buffer fails

Baz via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Oct 29 14:23:27 PDT 2014


On Wednesday, 29 October 2014 at 21:14:17 UTC, dcrepid wrote:
> I have this simple code:
> int main()
> {
>     import std.stdio;
>     char[4096] Input;
>     readln(Input);
>     //readln!(char)(Input);  // also fails
>     return 0;
> }
>
> I get these messages during compilation:
> test.d(39): Error: template std.stdio.readln cannot deduce 
> function from
> argument types !()(char[4096]), candidates are:
>  src\phobos\std\stdio.d(2818):
>     std.stdio.readln(S = string)(dchar terminator = '\x0a') if 
> (isSomeString!S)
>  src\phobos\std\stdio.d(2851):
>     std.stdio.readln(C)(ref C[] buf, dchar terminator = '\x0a') 
> if (isSomeChar!C && is(Unqual!C == C) && !is(C == enum))
>  src\phobos\std\stdio.d(2858):
>       std.stdio.readln(C, R)(ref C[] buf, R terminator) if 
> (isSomeChar!C && is(Unqual!C == C) && !is(C == enum) && 
> isBidirectionalRange!R && is(typeof(terminator.front == 
> (dchar).init)))
>
> Now, I'm used to 'buffer' meaning one thing, but here it seems 
> that buffer means something more akin to a 'sink' object, or a 
> forced dynamic array type?  Is there some way I can avoid 
> dynamic allocations?
>
> Thanks!

try this instead

------
module runnable;

import std.stdio;

void main(string args[])
{
     char[] Input;
     Input.length = 4096;
     readln(Input);
}
------

Your original sample does not compile because `char[4096]` is a
static array and does not verifies the redln() template 
constraints,
e.g input range, forward range etc.

Another option would be to slice Input:
----
readln(Input[0..$-1]);
----


More information about the Digitalmars-d-learn mailing list