On 14/12/2016 4:42 AM, Namal wrote:
Hello, comming from C++, I find it hard to remember and understand how
reading from file should be done in D. Especially since I am not very
good in functional programming. So I have a file which looks like this:
1,2,3,4
5,6,7,8
9,11,11,12
and so on
How could I read it row by row and create an array accordingly without
reading the comma?
import std.stdio : File;
import std.algorithm : splitter, count;
import std.conv : to;
foreach(line; File("myfile.csv", "r").byLine) {
int[] linetemp;
linetemp.length = line.count(",") + 1;
size_t i;
foreach(num; line.splitter(",")) {
linetemp[i] = to!int(num);
i++;
}
// ... = linetemp.dup;
}
This could be done a lot simpler especially with std.csv help for your
case. But this should work with only one allocation per line.