CSV Data to Binary File
TJB via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Aug 7 08:13:59 PDT 2014
On Thursday, 7 August 2014 at 15:11:48 UTC, TJB wrote:
> I am trying to read data in from a csv file into a struct, and
> then turn around and write that data to binary format.
>
> Here is my code:
>
> import std.algorithm;
> import std.csv;
> import stdio = std.stdio;
> import std.stream;
>
> align(1) struct QuotesBin
> {
> int qtim;9 int bid;
> int ofr;
> int bidsiz;
> int ofrsiz;
> short mode;
> char[1] ex;
> char[4] mmid;
> }
>
> void main()
> {
> string infile = "temp.csv";
> string outfile = "temp.bin";
> Stream fin = new BufferedFile(infile);
> Stream fout = new BufferedFile(outfile, FileMode.Out);
>
> foreach(ulong n, char[] line; fin)
> {
> auto record = csvReader!QuotesBin(line).front;
> fout.writeExact(&record, QuotesBin.sizeof);
> }
>
> fin.close();
> fout.close();
> }
>
> Here is a snippet of my csv data:
>
> 34220, 370000, 371200, 1, 1, 12, N,
> 34220, 369000, 372500, 1, 11, 12, P,
> 34220, 370000, 371200, 1, 2, 12, N,
> 34220, 370000, 371100, 1, 33, 12, N,
> 34220, 369400, 371100, 6, 3, 12, P,
> 34220, 370000, 371200, 1, 2, 12, N,
> 34220, 369300, 371200, 9, 2, 12, N,
> 34220, 369300, 371200, 5, 2, 12, N,
> 34220, 368900, 371200, 13, 2, 12, N,
> 34220, 368900, 371100, 13, 1, 12, N,
>
> For some reason this fails miserably. Can anyone help me out as
> to why? What do I need to do differently?
>
> Thanks,
> TJB
Some of the code got messed up when I pasted. Should be:
align(1) struct QuotesBin
{
int qtim;
int bid;
int ofr;
int bidsiz;
int ofrsiz;
short mode;
char[1] ex;
char[4] mmid;
}
Thanks!
More information about the Digitalmars-d-learn
mailing list