I have a file defined like this
ID TEXT
1 XXXX
2 XXXX
3 XXXX
4 XXXX
5 XXXX
6 XXXX
7 XXXX
8 XXXX
9 XXXX
10 XXXX
And the class for the filehelper defined like this
[DelimitedRecord("\t")]
public class TestItem
{
public int Id;
public string Text;
}
I read the file with the following code
FileHelperEngine<TestItem> eng = new FileHelperEngine<TestItem>();
using (var file = new FileStream("FILEPATH", FileMode.Open, FileAccess.Read))
{
//I've declared like this because filehelper close the reader after each iteration
StreamReader reader = new StreamReader(file, Encoding.UTF8, false, 1024, true);
eng.Options.IgnoreFirstLines = 1;
TestItem[] content = null;
bool headerRead = false;
do
{
content = eng.ReadStream(reader, 2);
if (!headerRead)
{
headerRead = true;
eng.Options.IgnoreFirstLines = 0;
}
}
while (!reader.EndOfStream);
}
read, as you can see, 2 record each time, and ignore the firl line at the first iteration. But at the second iteration, i'm expecting to obtain record 3 and record 4, but, instead, i receive back record 5 and 6. Why this? How to solve this?
The problem is with your usage of the ReadStream function. It is designed to read a file for the maximum number of records and then be closed. As such, it recreates a ForwardReader on every call.
The way ForwardReader.ReadNextLine() works is to pass the current value back, and read in the next line read for processing. So what happens is this:
So, if you were only making the one call, everything would be fine and this would be as expected. However, because you make another call, this happens:
In some regards this is a bug, but in others its your usage of ReadStream that's not really correct. The better way would be to ditch the maximum number of records to read, and instead make use of the INotifyRead/INotifyWrite functionality if you need to process on a per record basis.