如何从位置 x 开始的二进制文件中读取 y 值的 Shorts?

发布于 2024-09-07 17:51:15 字数 46 浏览 12 评论 0原文

我需要从二进制文件中从特定位置开始读取一定数量的短(int16)数据点。谢谢!

I need to read a certain amount of short (int16) data points from a binary file, starting at a specific position. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫腻 2024-09-14 17:51:15

像这样的事情应该为你做:

private IEnumerable<Int16> getShorts(string fileName, int start, int count)
using(var stream = File.OpenRead(fileName))
{
   stream.Seek(start);
   var reader = new BinaryReader(stream);
   var list = new List<int16>(count);
   for(var i = 0;i<count;i++)
   {
      list.Add(reader.ReadInt16());
   }
}

这基本上就是 CAsper 在代码中编写的内容

Something like this should do it for you:

private IEnumerable<Int16> getShorts(string fileName, int start, int count)
using(var stream = File.OpenRead(fileName))
{
   stream.Seek(start);
   var reader = new BinaryReader(stream);
   var list = new List<int16>(count);
   for(var i = 0;i<count;i++)
   {
      list.Add(reader.ReadInt16());
   }
}

which is basically what CAsper wrote just in code

樱花落人离去 2024-09-14 17:51:15

您可以简单地调用传递给 BinaryReader 的 Stream 上的 Seek 方法到文件中要开始读取的位置。

然后,将流传递给 BinaryReader 后,您可以根据需要多次调用 ReadInt16 方法。

You can simply call the Seek method on the Stream that you pass to BinaryReader to the position in the file you want to start reading from.

Then, once you pass the stream to BinaryReader, you can call the ReadInt16 method as many times as you need to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文