Hi. I'm a D newbie(!) coming from a Fortran/C/Python background. I'm struggling with the many new concepts needed in order to make any sense out of the documentation or traceback messages (ranges/templates/...). For example, the std.csv documentation is great but all the examples read from a string rather than a file. I feel stupid but I'm having trouble with the simple step of modifying the examples to read from a file. I can read the whole file into a string in memory and then read the records from the string just fine with csvReader (example A below) or read a line at a time from the file and call csvReader using a single line (example B below), but neither solution is satisfactory. In practice I need to read files with
up to 80 million records so I'd like to understand how to do this
properly/efficiently.

tia, Gerald

Example A
=========
import std.stdio, std.file, std.csv;

void main()
{
    std.file.write("test.csv", "0,1,abc\n2,3,def");
    scope(exit) std.file.remove("test.csv");

    auto lines = readText!(string)("test.csv");

    struct Rec { int a,b; char[] c; }
    foreach (Rec r; csvReader!Rec(lines)) {
        writeln("struct -> ", r);
    }
}


Example B
=========
import std.stdio, std.file, std.csv;

void main()
{
    std.file.write("test.csv", "0,1,abc\n2,3,def");
    scope(exit) std.file.remove("test.csv");

    struct Rec { int a,b; char[] c; }
    Rec r;
    foreach (line; File("test.csv", "r").byLine) {
        r = csvReader!Rec(line).front;
        writeln("struct -> ", r);
    }
}

Output
======
struct -> Rec(0, 1, "abc")
struct -> Rec(2, 3, "def")

Reply via email to