Difference between revisions of "Read table data from file"

From D Wiki
Jump to: navigation, search
m
Line 11: Line 11:
 
     string[][] buffer;
 
     string[][] buffer;
  
     foreach (line; inputFile.byLines) {
+
     foreach (line; inputFile.byLines)
 
         buffer ~= split(line.dup, fieldSeparator);
 
         buffer ~= split(line.dup, fieldSeparator);
  

Revision as of 22:13, 10 April 2017

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.