Thread overview | ||||||
---|---|---|---|---|---|---|
|
May 06, 2007 readline from char[] | ||||
---|---|---|---|---|
| ||||
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 |
May 06, 2007 Re: readline from char[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jan Hanselaer | Jan Hanselaer wrote:
> 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?
Did you try a MemoryStream? (I have no idea about the performance)
|
May 06, 2007 Re: readline from char[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jan Hanselaer | On Sun, 6 May 2007 11:31:14 +0200, 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? > Here are two routines that I use ... // ----------------- import std.file; import std.string; enum GetOpt { Exists = 'e', // Must exist otherwise Get fails. Always = 'a' // Get always returns something, even if it's just // empty lines for a missing file. } // Read an entire file into a string. char[] GetText(char[] pFileName, GetOpt pOpt = GetOpt.Always) { char[] lFileText; if (std.file.exists( pFileName)) { lFileText = cast(char[]) std.file.read(pFileName); // Ensure last line has a EOL. if ( (lFileText.length == 0) || (lFileText[$-1] != '\n')) lFileText ~= std.path.linesep; } else if (pOpt == GetOpt.Exists) { throw new Exception( std.string.format("File '%s' not found.", pFileName)); } return lFileText; } // Read a entire file in to a set of lines (strings). char[][] GetTextLines(char[] pFileName, GetOpt pOpt = GetOpt.Always) { char[][] lLines; char[] lText; lText = GetText(pFileName, pOpt); lLines = std.string.splitlines( lText ); return lLines; } -- Derek Parnell Melbourne, Australia "Justice for David Hicks!" skype: derek.j.parnell |
May 06, 2007 Re: readline from char[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jan Hanselaer | 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); } } |
Copyright © 1999-2021 by the D Language Foundation