getting started with std.csv

yazd via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Apr 7 01:12:06 PDT 2015


On Tuesday, 7 April 2015 at 05:51:33 UTC, yazd wrote:
> On Tuesday, 7 April 2015 at 05:49:48 UTC, yazd wrote:
>> I got this to work with:
>>
>> ```
>> import std.stdio, std.file, std.csv, std.range;
>>
>> void main()
>> {
>> 	std.file.write("test.csv", "0,1,abc\n2,3,def");
>> 	scope(exit) std.file.remove("test.csv");
>>
>> 	static struct Rec { int a, b; char[] c; }
>>
>> 	auto file = File("test.csv", "r");
>> 	foreach (s; csvReader!Rec(file.byLine().joiner("\n")))
>> 	{
>> 		writeln("struct -> ", s);
>> 	}
>> }
>> ```
>>
>> I am not sure about using `file.byLine()` here, because 
>> `byLine` reuses its buffer, but this is working correctly (for 
>> some reason, anyone can comment?) as far as I tested.
>
> Btw, joiner is a lazy algorithm. In other words, it doesn't 
> join the whole file when it is called but only when needed. 
> This reduces the memory requirements as you won't need the 
> whole file in memory at once.

Replace `std.range` with `std.algorithm`.


More information about the Digitalmars-d-learn mailing list