read till EOF from stdin

kdevel kdevel at vogtner.de
Fri Dec 11 12:34:19 UTC 2020


On Friday, 11 December 2020 at 11:05:59 UTC, frame wrote:
> On Friday, 11 December 2020 at 02:31:24 UTC, kdevel wrote:
>>    auto s = cast (string) stdin.byChunk(4).join;
>>
>> As strace reveals the resulting program sometimes reads twice 
>> zero
>> characters before it terminates:
>>
>>    read(0, a                                         <-- A, 
>> return
>>    "a\n", 1024)                    = 2
>>    read(0, "", 1024)                       = 0       <-- ctrl+d
>>    read(0, "", 1024)                       = 0       <-- ctrl+d
>>
>> Any comments or ideas?
>
> I see expected behaviour here if you use a buffer of length 4. 
> I don't know what you want to achieve here.

Read till EOF.

> If you want to stop reading from stdin, you should check for 
> eof() instead.

My code cannot do that because the function byChunk has control 
over the
file descriptor. The OS reports EOF by returning zero from 
read(2). The
D documentation of byChunk [1] does not mention such a check for 
eof
either.

> You should not check yourself for the character.

Where did I do that here?

    auto s = cast (string) stdin.byChunk(4).join;

> eof() can be lock in by multiple ways and it is the only 
> correct way to handle all of them.

??

[1] https://linux.die.net/man/2/read
[2] https://dlang.org/phobos/std_stdio.html#byChunk


More information about the Digitalmars-d-learn mailing list