www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - readLine behavior on non seekable streams

I've noticed that calling readLine on a non seekable Stream (like
std.cstream.din) on a file using the dos '\r\n' end of line sequence,
will stop when '\r' is seen. And a second call to readLine will stop on
the following '\n'. The sequence '\r\n' is not eaten as a whole end of  
line sequence, like it's done for a seekable stream.

Here goes the code to show the problem:

$ cat file_vs_stdin.d
import std.stdio;
import std.cstream;

void dump_stream(Stream s)
{
writefln("----------");
while (!s.eof())
writefln(s.readLine());
writefln("----------");
}

int main (char[][] args)
{
if (args.length == 2) {
File f = new File(args[1]);
dump_stream(f);
f.close();
} else
dump_stream(din);
return 0;
}

$ dmd file_vs_stdin.d

If I test the program on a dos file called test.txt, containing 3 lines
terminated by '\r\n':

$ cat test.txt
one
two
three

$ ./file_vs_stdin test.txt
----------
one
two
three
----------

$ ./file_vs_stdin < test.txt
----------
one

two

three


----------

As you can see, when data are read from standard input, the end of line
sequence '\r\n' is not read as a whole, then input is treated as if it contained
6 lines. If you look carrefully, 7 lines are even printed, as
eof if not raised the same way between a seekable stream and a non-seekable
stream.

Is there a good reason to treat non-seekable streams this way?
Apr 24 2006