www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Reading input lines

reply "Bill Lear" <rael see.sig.com> writes:
I am trying to read lines from stdin and echo them to stdout.

I have come up with this:

import std.stdio;
import std.cstream;
import std.string;

void main () {
    while (!din.eof()) {
        char[] line = din.readLine();
        if (line == null) continue;
        writefln("%s", line);
    }
}

but am confused why I would need to check for null.  Is there
a better way to do this?


Bill
-- 
r * e *   * o * y * a * c * m
* a * l * z * p * r * . * o *
Jan 22 2007
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Bill Lear" <rael see.sig.com> wrote in message 
news:m33b621rvp.fsf pppp.zopyra.com...
I am trying to read lines from stdin and echo them to stdout.

 I have come up with this:

 import std.stdio;
 import std.cstream;
 import std.string;

 void main () {
    while (!din.eof()) {
        char[] line = din.readLine();
        if (line == null) continue;
        writefln("%s", line);
    }
 }

 but am confused why I would need to check for null.  Is there
 a better way to do this?

Do you? I took out the null check and it works fine, unless you're getting errors on Linux or something.
Jan 22 2007
parent reply "Bill Lear" <rael see.sig.com> writes:
"Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
 "Bill Lear" <rael see.sig.com> wrote in message 
 news:m33b621rvp.fsf pppp.zopyra.com...
I am trying to read lines from stdin and echo them to stdout.

 I have come up with this:

 import std.stdio;
 import std.cstream;
 import std.string;

 void main () {
    while (!din.eof()) {
        char[] line = din.readLine();
        if (line == null) continue;
        writefln("%s", line);
    }
 }

 but am confused why I would need to check for null.  Is there
 a better way to do this?

Do you? I took out the null check and it works fine, unless you're getting errors on Linux or something.

Yes, this is under Linux, dmd version 1.00. If I run the program, with the text of the program as input, without the null check, it prints out an additional blank line for every line in the file. Bill -- r a e l z o p y r a . c o m
Jan 23 2007
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Bill Lear" <rael see.sig.com> wrote in message 
news:m3hcuhzkpe.fsf lisa.zopyra.com...

 Yes, this is under Linux, dmd version 1.00.  If I run the program,
 with the text of the program as input, without the null check, it
 prints out an additional blank line for every line in the file.

Hmm, that doesn't happen under Windows. I guess it's a platform-specific issue. Might want to post it in Bugzilla (I can't find any issues about it there already).
Jan 23 2007
parent Thomas Kuehne <thomas-dloop kuehne.cn> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jarrett Billingsley schrieb am 2007-01-24:
 "Bill Lear" <rael see.sig.com> wrote in message 
 news:m3hcuhzkpe.fsf lisa.zopyra.com...

 Yes, this is under Linux, dmd version 1.00.  If I run the program,
 with the text of the program as input, without the null check, it
 prints out an additional blank line for every line in the file.

Hmm, that doesn't happen under Windows. I guess it's a platform-specific issue. Might want to post it in Bugzilla (I can't find any issues about it there already).

http://d.puremagic.com/issues/show_bug.cgi?id=881 Thomas -----BEGIN PGP SIGNATURE----- iD8DBQFFtx+CLK5blCcjpWoRAolCAJ46cb6NwuO5f37DNuZkkjIQFhxj5gCgiIA7 TIJ3z+QLpljw7OfvuEg223Y= =iV/P -----END PGP SIGNATURE-----
Jan 24 2007