Dog*_*ert 10
你可以使用:file.position/2和:file.read/2.
附:
$ seq 10 > 10.txt
Run Code Online (Sandbox Code Playgroud)
和代码:
{:ok, file} = :file.open("10.txt", [:read, :binary])
:file.position(file, 5)
IO.inspect :file.read(file, 10)
Run Code Online (Sandbox Code Playgroud)
输出是:
{:ok, "\n4\n5\n6\n7\n8"}
Run Code Online (Sandbox Code Playgroud)
那是从第6个字节开始的10个字节.
如果您阅读文档将会很方便。例如file:pread/2,3。
read(File, Start, Length) ->
{ok, F} = file:open(File, [binary]),
try file:pread(F, [{Start, Length}]) of
{ok, [Data]} -> Data
after file:close(F)
end.
Run Code Online (Sandbox Code Playgroud)
这将是 Hynek 共享转录到 Elixir 的代码。我只是将其作为答案发布,因为放入评论有点长。
def read(file, start, length) do
{ok, f} = :file.open(file, [:binary])
{ok, data} = :file.pread(f, start, length)
:file.close(f)
data
end
Run Code Online (Sandbox Code Playgroud)
是的,如果能将它放在 Elixir 文件模块中就好了。如果你真的想要它@CharlesO,为什么不继续创建一个拉取请求呢?Jose 和其他核心提交者是我在多年的软件开发过程中遇到的最友好的人。