Read table data from file

From D Wiki
Revision as of 08:13, 5 September 2013 by Moondog (talk | contribs) (Created page with "== Reading tabular data from file == To read in a text file with records in rows, where fields are separated by a separator (e.g. tab, whitespace), this code might help: <sy...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Reading tabular data from file

To read in a text file with records in rows, where fields are separated by a separator (e.g. tab, whitespace), this code might help:

 1 import std.stdio;
 2 import std.array;
 3 
 4 auto readInData(File inputFile, string fieldSeparator)
 5 {
 6     string[][] buffer;
 7 
 8     foreach (line; inputFile.byLines) {
 9         buffer ~= split(line.dup, fieldSeparator);
10 
11     return buffer;
12 }


The not so obvious usage of .dup property is necessary here in order to avoid memory corruption of the output multidimensional string array. There are couple of reasons for this:

  • the LineReader defined with File.byLine() reuses its buffer for efficiency and
  • split() function is optimized to return slices into its input buffer (line in this case) instead of copying each substring to output.

Without the line being duplicated the output buffer gets overwritten in every iteration.

Credits

The information provided here was first discussed on this thread.