Newbie: Error parsing csv file with very long lines

rikki cattermole via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Apr 23 03:44:19 PDT 2016


On 23/04/2016 10:40 PM, salvari wrote:
> Hello all!
>
> I'm trying to read a csv file (';' as separator) with very long lines.
>
> It seems to be really simple, I read the columns name with no problem.
> But as soon as the program parses the first line of data, the array
> containing the columns names seems to be overwrited.
>
> I'm using dmd: DMD64 D Compiler v2.071.0
>
> My code:
>
> import std.stdio;
> import std.algorithm;
> import std.array;
>
> char[][] columns;
>
>
> void main() {
>   LINE:foreach(line; stdin.byLine()){
>      if(line.startsWith("Interfaz")){
>        writeln("IN HERE");
>        columns = line.split(";");
>        writeln(columns);               // Everything seems to be ok
>        continue;
>      } else{
>        auto linedata = line.split(";");
>        writefln("My line: %s", line);        // Fine.
>        writefln("LineData: %s", linedata);   // Fine. Line data is ok
>        writefln("Columns: %s", columns);     // Wrong!!! columsn array
>                                              // contains garbage data
>                                              // from linedata
>      }
>    }
> }

Its probably using a buffer.
columns = line.dup.split(";");
Should fix it.


More information about the Digitalmars-d-learn mailing list