Read csv data into a struct

tjb broughtj at gmail.com
Fri Mar 2 13:47:53 PST 2012


Hello,

I am trying to read some data from a csv file into a struct.  I'm just learning to use
D.  This little bit of code reads the data and prints to standard output just fine:

import std.stdio : writeln;
import std.stream;

void main() {
  auto fin = new File("temp.csv");
  char[] line;

  int count;
  while(!fin.eof()) {
    line = fin.readLine();
    writeln(line);
  }

  auto dt1 = SysTime.from
}

But I don't know how to parse the input to a structure. Another complication is that
some of my fields are dates and times (possibly with non-standard formats). Can you
suggest an approach?

Here are the first few lines of output from the code above:

1/2/08,9:30:07,Q,70780,X,A,4.75,61,5,71,N,R,36.68,1,36.73,1
1/2/08,9:30:08,Q,73028,X,A,4.75,61,5,61,N,R,36.68,1,36.73,1
1/2/08,9:30:09,Q,79462,X,A,4.75,73,5,88,N,R,36.6,2,36.73,1
1/2/08,9:30:10,Q,86182,A,,4.75,11,5,11,N,R,36.61,2,36.71,1
1/2/08,9:30:10,Q,87054,X,A,4.75,73,5,76,N,R,36.61,2,36.71,1

Thanks!

TJB


More information about the Digitalmars-d-learn mailing list