StreamReader没有读取我的整个文件?

时间:2012-05-03 16:45:05

标签: c# .net visual-studio netduino

解决:

  

SD卡上的文件在文件中的那一点有一堆NULL。


我正在使用Netduino Plus从SD卡上的文件中读取文本。我正在使用C#.NET Micro Framework 4.2和FileReader / StreamReader来完成它。我读过StreamReader缓冲区是512字节长。您是否只能使用StreamReader读取512字节的数据,那就是它?这就是为什么我想知道并解释我的问题...

以下是我正在阅读的文件示例...

40
3,241,17,17,1000,2000
3,14,92,223,1000,2000
3,101,229,12,1000,2000
3,16,215,228,1000,2000
3,50,240,11,1000,2000
3,232,213,10,1000,2000
3,234,219,219,1000,2000
3,202,13,222,1000,2000
3,240,5,65,1000,2000
3,25,234,3,1000,2000
3,236,5,164,1000,2000
3,26,229,12,1000,2000
3,225,217,18,1000,2000
3,8,229,216,1000,2000
3,49,0,7,1000,2000
3,12,99,190,1000,2000
3,222,7,226,1000,2000
3,12,221,208,1000,2000
3,4,37,227,1000,2000
3,4,122,48,1000,2000
3,88,181,192,1000,2000
3,1,17,222,1000,2000
3,56,235,19,1000,2000
3,236,15,101,1000,2000
3,13,175,231,1000,2000
3,229,218,17,1000,2000
3,9,74,239,1000,2000
3,10,233,17,1000,2000
3,12,73,227,1000,2000
3,234,3,3,1000,2000
3,7,128,110,1000,2000
3,5,209,241,1000,2000
3,8,61,229,1000,2000
3,237,1,238,1000,2000
3,228,19,19,1000,2000
3,16,228,92,1000,2000
3,243,206,14,1000,2000
3,193,3,220,1000,2000
3,236,7,7,1000,2000
3,115,236,7,1000,2000

- 第一行表示要遵循的行数。 - 以下行中的第一个数字表示将从该行读取多少数据项。 (在这个例子中,每行有3个数据元素) - 该行的最后两个数字是以ms为单位的时间。

我的代码读取了大量文件并停止。

40
3,241,17,17,1000,2000
3,14,92,223,1000,2000
3,101,229,12,1000,2000
3,16,215,228,1000,2000
3,50,240,11,1000,2000
3,232,213,10,1000,2000
3,234,219,219,1000,2000
3,202,13,222,1000,2000
3,240,5,65,1000,2000
3,25,234,3,1000,2000
3,236,5,164,1000,2000
3,26,229,12,1000,2000
3,225,217,18,1000,2000
3,8,229,216,1000,2000
3,49,0,7,1000,2000
3,12,99,190,1000,2000
3,222,7,226,1000,2000
3,12,221,208,1000,2000
3,4,37,227,1000,2000
3,4,122,48,1000,2000
3,88,181,192,1000,2000
3,1,17,222,1000,2000
3,56,235,19,1000,2000
3,236,15,101,1000,2000
3,13,175,231,1000,2000
3,229,218,17,1000,2000
3,9,74,239,1000,2000
3,10,233,17


other lines are not read

它错过了这个readline调用的两个时间值,并且代码在System.IndexOutOfRangeException上的代码中分配了time2,因为linearry是5个元素长(null不能很好地转换为int lol )看起来像......

linearray[0] = "3"
linearray[1] = "10"
linearray[2] = "233"
linearray[3] = "17"
linearray[4] = ""

而不是像其他线条一样长6个元素,看起来像....

linearray[0] = "3"
linearray[1] = "10"
linearray[2] = "233"
linearray[3] = "17"
linearray[4] = "1000"
linearray[5] = "2000"

所有前面的行都读得很好,linearray包含它应该的所有数据。我没有正确使用StreamReader吗?

这是代码....

            FileStream fs2 = new FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.None);
            StreamReader sr = new StreamReader(fs2);

            line = sr.ReadLine();

            numLines = Convert.ToInt32(line);

            line = "";
            for (int i = 0; i < numLines; i++)
            {
                line = sr.ReadLine();
                string[] linearray = line.Split(comma);
                numDataElements[i] = int.Parse(linearray[0]);

                for (int j = 1; j <= numDataElements[i]; j++)
                {
                    readData[i][j] = byte.Parse(linearray[j]);
                }
                //clear the rest of the channels
                for (int j = (numDataElements[i]+1); j <= MAXCHANNELS; j++)
                {
                    data[i][j] = (byte)0;
                }

                time1[i] = Convert.ToInt32(linearray[linearray.Length - 2]);
                time2[i] = Convert.ToInt32(linearray[linearray.Length - 1]);

            }

谢谢!

1 个答案:

答案 0 :(得分:0)

回答这个问题,上面的代码没有任何问题。我在FTP服务器库中发现了一个错误。它也使用512字节缓冲区,只写缓冲区是完全。因此,部分已满的第二个缓冲区从未写入传入文件。我修复了FTP库代码,一切都很好!