phobos: What type to use instead of File when doing I/O on streams?
Ali Çehreli via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Nov 9 06:48:35 PST 2015
On 11/09/2015 01:48 AM, J.Frank wrote:
> My question is now:
> How can I make this work without using deprecated stuff?
>
>
> import std.cstream;
>
> void foo(InputStream in_stream, OutputStream out_stream)
> {
> // in_stream.seek(3); // compile error - good :)
>
> char[] line;
> while ((line = in_stream.readLine()) !is null)
> out_stream.writeLine(line);
> }
>
> void main(string[] args)
> {
> foo(din, dout);
> }
>
You don't need the template constraints but it is good practice:
import std.stdio;
import std.range;
void foo(I, O)(I in_stream, O out_stream)
if (isInputRange!I &&
isOutputRange!(O, ElementType!I)) {
// in_stream.seek(3); // compile error - good :)
foreach (element; in_stream) {
out_stream.put(element);
}
}
void main(string[] args)
{
// Also consider .byLine, which is faster and risky
foo(stdin.byLineCopy,
stdout.lockingTextWriter);
}
Ali
More information about the Digitalmars-d-learn
mailing list