www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - readline from char[]

reply "Jan Hanselaer" <jan.hanselaer gmail.com> writes:
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
next sibling parent Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
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
prev sibling next sibling parent Derek Parnell <derek psych.ward> writes:
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?
 

// ----------------- 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
prev sibling parent Tomas Lindquist Olsen <tomas famolsen.dk> writes:
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); } }
May 06 2007