readline from char[]

Tomas Lindquist Olsen tomas at famolsen.dk
Sun May 6 06:06:02 PDT 2007


Jan Hanselaer wrote:

> Hi
> 
> Is it possible to read lines from a char[] buffer? I ask this because
> reading lines directly from a file (with a BufferedStream) seems to be
> very slow.
> So when I can first load my file
> 
> char[] file = cast(char[])std.file.read("filename");
> 
> and then read lines from file that would go faster I presume.
> 
> But I don't see a way to do this.
> 
> Anyone with an idea for this problem?
> 
> Thanks in advance.
> 
> Jan

You can do something like:

import std.file;
import std.stdio;
import std.string;

void main()
{
        char[] file = cast(char[])std.file.read("filename");
        foreach(i,line; std.string.splitlines(file))
        {
                std.stdio.writefln("#",i," = ",line);
        }
}



More information about the Digitalmars-d-learn mailing list