StreamReader.ReadLine() throws IndexOutOfRangeException after reading

I’m having an issue with reading data from an SD card. Using EMX dev kit and 4.2 RC2.

This is the code:

                //Load old data from SD card
                if (File.Exists(rootDirectory + @ "\TempHumid.csv"))
                {//load data
                    StreamReader FileReader = new StreamReader(rootDirectory + @ "\TempHumid.csv");
                    if (FileReader.BaseStream.Length > (35 * 800))
                    {
                        FileReader.BaseStream.Seek(-(35 * 800), SeekOrigin.End);
                    }
                    else
                    {//Go to start
                        FileReader.BaseStream.Seek(0, SeekOrigin.Begin);
                    }
                    string data;
                    string[] param;
                    string[] date;
                    do
                    {
                        data = FileReader.ReadLine();
                        Debug.Print(data);
                        param = data.Split(',');
                        date = param[0].Split('-', ' ', ':');
                        //altitudeGraph.Plot(double.Parse(param[1]), double.Parse(param[2]), (new DateTime(int.Parse(date[0]), int.Parse(date[1]), int.Parse(date[2]), int.Parse(date[3]), int.Parse(date[4]), int.Parse(date[5]))).ToString("H:mm:ss"));
                    }
                    while (FileReader.EndOfStream == false);

                    FileReader.Close();
                }

The file is around 28KB big, and this code gets about halfway through, then throws this error on FileReader.ReadLine()

[quote] #### Exception System.IndexOutOfRangeException - 0xa9000000 (1) ####
#### Message:
#### System.IO.StreamReader::Peek [IP: 001c] ####
#### System.IO.StreamReader::ReadLine [IP: 005e] ####
#### HumidityGraph.Program::Main [IP: 006d] ####
A first chance exception of type ‘System.IndexOutOfRangeException’ occurred in System.IO.dll
An unhandled exception of type ‘System.IndexOutOfRangeException’ occurred in System.IO.dll[/quote]

Any Ideas?

There was a known issue with method. Looks like it hasn’t been fixed. Search for ReadLineEx from William on the forum.

Found this: http://www.tinyclr.com/forum/topic?id=1620&page=1

So a year later, one minor version and two RCs and it’s still there? :slight_smile:

Yep, that is the one. So did it help?

Nope. Dies in the same place of the file, with the same error. But now it dies at:

if (sr.Peek() == 10)
{
         sr.Read();
}

on the " if (sr.Peek() == 10)" statement…

I ended up using normal read as I know the length of the records…

public override int Read(
	char[] buffer,
	int index,
	int count
)

Runs fine now… :slight_smile:

I also spend some time in this pitfall today :slight_smile:

If you look at the attached screenshot you see that streamreader is doing fine until line 110
If i print the next line (after the Indexoutofrange err) in the immediate window you see this a blank string “” for line 120
and from there each next print is reading fine again…

So i also give “ReadLineEx” a try, with a failure as said by GMod

But if you modify this code to :


        public static string ReadLineEx(StreamReader sr)
        {
            int newChar = 0;
            int bufLen = 512; // NOTE: the smaller buffer size.
            char[] readLineBuff = new char[bufLen];
            int growSize = 512;
            int curPos = 0;
            while ((newChar = sr.Read()) != -1)
            {
                if (curPos == bufLen)
                {
                    if ((bufLen + growSize) > 0xffff)
                    {
                        throw new Exception();
                    }
                    char[] tempBuf = new char[bufLen + growSize];
                    Array.Copy(readLineBuff, 0, tempBuf, 0, bufLen);
                    readLineBuff = tempBuf;
                    bufLen += growSize;
                }
                readLineBuff[curPos] = (char)newChar;
                if (readLineBuff[curPos] == '\n')
                {
                    return new string(readLineBuff, 0, curPos);
                }
                if (readLineBuff[curPos] == '\r')
                {
                        sr.Read();
                    return new string(readLineBuff, 0, curPos);
                }
                curPos++;
            }

            if (curPos == 0) return null; // Null fix.
            return new string(readLineBuff, 0, curPos);
        }

Then this working just fine :slight_smile:

1 Like

Thanks for this David, it works perfectly.

I was also having problems with Streamreader.Readline and larger files. I was about to start working on my own solution when I found this (unsurprisingly on this forum! ;)) so you’ve saved me the trouble.

Many thanks. ;D

Thanks :slight_smile: always nice to hear feedback is useful.

Looking at the number of views this thread has, I would guess that lots of people have found your code useful…

As a newbie this bug had me pulling my hair out (what little I have left) for days before I found this thread! :wall:

This issue still exists in 4.2

And in 4.3 too if I remember right.
I think it might be fixed for 4.4.

It exists in 4.3 and I still have to use the fix.