digitalmars.D.learn - readline from char[]
- Jan Hanselaer (11/11) May 06 2007 Hi
- Frits van Bommel (2/14) May 06 2007 Did you try a MemoryStream? (I have no idea about the performance)
- Derek Parnell (44/59) May 06 2007 Here are two routines that I use ...
- Tomas Lindquist Olsen (13/31) May 06 2007 You can do something like:
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
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
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
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. JanYou 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)) { } }
May 06 2007