std.csv.csvReader operating on File.byLine()

Ali Çehreli acehreli at yahoo.com
Sun Oct 7 12:20:11 PDT 2012


On 10/07/2012 11:21 AM, Rene Zwanenburg wrote:
 > Hi,
 >
 > To my understanding, The csv reader in std.csv is able to operate on any
 > kind of input range.

The error message took me to the definition of the function. The 
template constraints require that the element type of the range must be 
a dchar:

// ...
   if(isInputRange!Range && is(ElementType!Range == dchar)
// ...

In your case, the element type of byLine() is a string. (I think.)

 > However I can only get it to work on strings.
 > Please take a look at the following line of code. Did I make some 
mistake?
 >
 > auto reader = csvReader!(Tuple!(int, int, float))(someFile.byLine());
 >
 > When I try to compile that, the compiler output is:
 >
 > main.d(24): Error: template std.csv.csvReader does not match any
 > function template declaration
 > C:\D\dmd2\windows\bin\..\..\src\phobos\std\csv.d(283): Error: template
 > std.csv.csvReader cannot deduce template function from argument types
 > !(Tuple!(int,int,float))(ByLine!(char,char))
 > main.d(24): Error: template instance csvReader!(Tuple!(int,int,float))
 > errors instantiating template
 >
 > As a workaround I could use readText, but that's dirty and won't work
 > for large csv files.

If deneme.txt contains this:

1,2,3.5
6,7,8.5

This works:

import std.stdio;
import std.csv;
import std.typecons;

void main()
{
     auto someFile = File("deneme.txt");

     foreach (line; someFile.byLine()) {
         auto reader = csvReader!(Tuple!(int, int, float))(line);
         foreach (record; reader) {
             writeln(record);
         }
     }
}

Ali



More information about the Digitalmars-d-learn mailing list