StreamReader 没有读取我的整个文件

本文关键字:文件 我的 读取 StreamReader | 更新日期: 2023-09-27 18:32:59

已解决:

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 个数据元素(-行上的最后两个数字是以毫秒为单位的时间。

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

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

它错过了此读取行调用中的两个时间值,并且代码在下面的 System.IndexOutOfRangeException 代码中分配 time2 时中断,因为线性长度为 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"

前面的所有行都读取得很好,线性阵列包含它应该包含的所有数据。 我沒有正確使用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]);
            }

谢谢!

StreamReader 没有读取我的整个文件

只是为了回答这个问题,上面的代码没有任何问题。 我在 FTP 服务器库中发现了一个错误。 它还使用 512 字节缓冲区,并且只写入缓冲区是 FULL。 因此,部分已满的第二个缓冲区永远不会写出到传入文件中。 我修复了FTP库代码,一切都很好!