What is the correct way to read a text file into an array of strings? I found the following on Rosetta Stone:
string[] readLines(string filename) {
auto f = File(filename);
scope(exit) f.close();
string[] lines;
foreach (str; f.byLine) {
lines ~= str.idup;
}
return lines;
}
but it looks like it makes one array size per line, which is pretty inefficient. I could track the number of rows read and resized by using the standard doubling method
int i = 0;
foreach (str; f.byLine) {
if (lines.length <= i + 1) {
lines.length = lines.length * 2 + 1;
}
lines[i] = str.idup;
i++;
}
lines.length = i;
but this is enough template code, which I have to wonder if I don’t notice something in the standard library that is already doing this for me.
Edit: giving fwend comment more visibility: this article details how the array allocator works and why adding is handled efficiently by the runtime
source
share