Skip to content

Error when reading file with Quoted field at end of first row and SkipEmptyLines=false #73

@bornfromanegg

Description

@bornfromanegg

If you attempt to read a file containing the following:

North,"South"
East,West

using the code:

var csvReader = new CsvReader(new StreamReader(<insert-file-here>), hasHeaders: false)
{
  SupportsMultiline = false,
  SkipEmptyLines = false
};

while (csvReader.ReadNextRecord())
{
  var s = csvReader[0];
}

Then you receive the error:

LumenWorks.Framework.IO.Csv.MissingFieldCsvException: The CSV appears to be corrupt near record '1' field '0 at position '15'. Current raw data : 'North,"South"
    East,West'.
       at LumenWorks.Framework.IO.Csv.CsvReader.HandleParseError(MalformedCsvException error, Int32& pos) in C:\Devel\OpenSource\CsvReader\code\LumenWorks.Framework.IO\Csv\CsvReader.cs:line 1800
       at LumenWorks.Framework.IO.Csv.CsvReader.HandleMissingField(String value, Int32 fieldIndex, Int32& currentPosition) in C:\Devel\OpenSource\CsvReader\code\LumenWorks.Framework.IO\Csv\CsvReader.cs:line 1875
       at LumenWorks.Framework.IO.Csv.CsvReader.ReadField(Int32 field, Boolean initializing, Boolean discardValue) in C:\Devel\OpenSource\CsvReader\code\LumenWorks.Framework.IO\Csv\CsvReader.cs:line 1193
       at LumenWorks.Framework.IO.Csv.CsvReader.get_Item(Int32 field) in C:\Devel\OpenSource\CsvReader\code\LumenWorks.Framework.IO\Csv\CsvReader.cs:line 644
       at ConsoleApp4.Program.Main(String[] args) in C:\Users\paul8828\source\repos\ConsoleApp4\ConsoleApp4\Program.cs:line 31

Looking at the stacktrace suggests that the reader thinks it is trying to read a missing field. And in fact, if you set MissingFieldAction to ReplaceByNull, then the error goes away, but the reader actually reads three rows. An entirely blank row is "read" inbetween row 1 and 2 in the source file.

I would like to be able to read using SkipEmptyLines=false, but that is a problem if the reader sometimes inserts extra blank rows.

Note this only seems to happen under the exact conditions described.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions