gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 16:32:25 24 4
gpt4 key购买 nike

已解决:

The file on the SD card has a bunch of NULLs at that point in the file.


我正在使用 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 调用中的两个时间值,并且代码在下面的代码中的 time2 分配上中断了 System.IndexOutOfRangeException 因为 linearry 是 5 个元素长(null 不能很好地转换为 int 哈哈) 并且看起来像....

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]);

}

谢谢!

最佳答案

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

关于c# - StreamReader 没有读取我的整个文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10435778/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com