File() vs. std.cstream.din
Gerome Fournier
Gerome_member at pathlink.com
Thu Apr 20 13:36:22 PDT 2006
The following code doesn't behave the same if I output a file by
opening it, or through std.cstream.din. I get an extra line when using
std.cstream.din, and I'd like to understand why.
Here goes the code first to show the problem:
$ cat file_vs_stdin.d
import std.cstream;
void dump_stream(Stream s)
{
while (!s.eof()) {
char[] line = s.readLine();
printf("Line: %.*s\n", line);
}
}
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
gcc file_vs_stdin.o -o file_vs_stdin -lphobos -lpthread -lm
Here goes a sample file to perform tests:
$ cat test.txt
one
two
three
$ ./file_vs_stdin test.txt
Line: one
Line: two
Line: three
$ ./file_vs_stdin < test.txt
Line: one
Line: two
Line: three
Line:
Why do I get an extra line here?
More information about the Digitalmars-d
mailing list