File() vs. std.cstream.din

Regan Heath regan at netwin.co.nz
Sun Apr 23 15:19:02 PDT 2006


On Sat, 22 Apr 2006 15:01:40 +0000 (UTC), Gerome Fournier  
<Gerome_member at pathlink.com> wrote:
> In article <ops8bt9xwe23k2f5 at nrage.netwin.co.nz>, Regan Heath says...
>>
>> My guess is that it is related to when eof is flagged. In the File case  
>> it
>> reads the last line and flags eof, in the case of din it has to read  
>> past
>> the end to flag eof.
>
>> If you modify the code to be:
>>
>> void dump_stream(Stream s)
>> {
>> 	while (!s.eof()) {
>> 		char[] line = s.readLine();
>> 		if (line !is null) printf("Line: %.*s\n", line);
>> 	}
>> }
>
> Thanks for your comment. You're right telling that concerning din, looks  
> like
> it's looking past the end of file to flag eof. Sounds like a kind of bug  
> to me,
> as I would expect the same behaviour between a file based stream (a  
> seekable
> stream), and a din based stream (a non seekable stream).
>
>> If you modify the code to be:
>>
>> void dump_stream(Stream s)
>> {
>> 	while (!s.eof()) {
>> 		char[] line = s.readLine();
>> 		if (line !is null) printf("Line: %.*s\n", line);
>> 	}
>> }
>
> Unfortunatly this change doesn't work when you have empty lines in your  
> file,
> as they're skipped. Reading a line containing a single '\n' will give a  
> null
> value to variable line through the call to readLine.

Ideally it should return a line which is not null but has a length of 0  
i.e. an empty line. Until it does, it looks like you're stuck with:

while(true) {
   char[] line = s.readLine();
   if (line is null) {
     if (s.eof) break;
     line = "";
   }
   writefln("Line: %s",line);
}

or similar.

> I've even 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.

That's a bug that needs to be fixed.

Regan



More information about the Digitalmars-d mailing list