.net - fsharp BinaryReader 如何读取文件末尾?

标签 .net f#

我想尝试基本的文件加密和压缩,所以我的第一个问题是 ByteReader 是否是适合该作业的正确阅读器?我的第二个最重要的问题是:使用二进制阅读器时如何循环直到文件末尾?有没有办法像 How read a file into a seq of lines in F# 中所述的 StreamReader 一样做到这一点?

我想做以下事情:

let reader = new BinaryReader(File.Open("anyFile",FileMode.Open,FileAccess.Read)
let mutable data = []
while -condition i can't find- do
   data <- [reader.readBytes()] :: data
//do fancy things with the achieved data

最佳答案

如果您只想将流中的所有数据读取到内存,则方法 Stream.CopyTo可能是最简单的解决方案:

use stream = File.Open("anyFile", FileMode.Open, FileAcces.Read)
use mem = new MemoryStream()
stream.CopyTo mem
let data = mem.ToArray()

关于.net - fsharp BinaryReader 如何读取文件末尾?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49457111/

相关文章:

f# - 如何使用 F# 实现此通用 C# 接口(interface)?

c# - 处理函数代码中可变性的策略

c# - 根据另一个列表或不同大小对列表进行排序

c# - 如何在 .NET/ASMX 中更改 WebService 响应的标签名称?

C# 生成图像中没有灰度值/噪声的二进制掩码?

f# - 函数 [x] -> ... 是如何工作的

f# - 如何用循环引用声明不可变图?

c# - 什么是 F# 尾调用?为什么它比 C# 有性能提升?

c# - 使用带有 bindinglist<business obj> 的绑定(bind)源作为数据源有什么好处?

c# - WCF 服务 - 请求超出限制