Difference between chunks(stdin, 1) and stdin.rawRead?

jms jersni at gmail.com
Thu Mar 28 02:30:11 UTC 2024


Why in the below silly program am I reading both the \r and \n 
characters when using rawRead in block a, but when looping by 1 
byte chunks in block b only appear to be reading the \n 
characters?

I'm on Windows 11 using DMD64 D Compiler v2.107.1 if that 
matters, but I'm thinking this maybe has something to do with 
stdin in general that I'm not aware of. Any pointers to 
understanding what's going on would be appreciated.

import std.stdio;

void main() {
     int i;
a: {
         i = 0;
         writeln("\nin a");
         ubyte[1] buffer;
         while (true) {
             i++;
             stdin.rawRead(buffer);
             if (buffer[0] == 13) {
                 write("CR");
             } else if (buffer[0] == 10) {
                 write("LF");
             }
             if (i > 5) {
                 goto b;
             }

         }
     }
b: {

         writeln("\n\nin b");
         i = 0;
         foreach (ubyte[] buffer; chunks(stdin, 1)) {
             i++;
             if (buffer[0] == 13) {
                 write("cr");
             } else if (buffer[0] == 10) {
                 write("lf");
             }
             if (i > 5) {
                 goto a;
             }
         }
     }

}



Output:
in a

CRLF
CRLF
CRLF

in b

lf
lf
lf
lf
lf
lf
in a


More information about the Digitalmars-d-learn mailing list