解决:
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
它错过了这个 readline 调用上的两个时间值,并且代码在 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"
前面的所有行都可以很好地读取,并且 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]);
}
谢谢!