如何从OCaml的文本文件中读取行? [英] How do I read in lines from a text file in OCaml?
本文介绍了如何从OCaml的文本文件中读取行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我到目前为止所拥有的.这不是您所需要的吗?我不断收到错误错误:未绑定模块标准"
This is what I have so far. Isn't this all that you need? I keep getting the error "Error: Unbound module Std"
let r file =
let chan = open_in file in
Std.input_list (chan)
推荐答案
如果您没有 Extlib 已安装(显然您不是基于上面的错误消息),然后通常会执行以下操作:
If you don't have Extlib installed (and apparently you don't based on the error message above), then generally it's done something like this:
let read_file filename =
let lines = ref [] in
let chan = open_in filename in
try
while true; do
lines := input_line chan :: !lines
done; !lines
with End_of_file ->
close_in chan;
List.rev !lines ;;
如果您有Extlib:
If you do have Extlib:
let read_file filename =
let chan = open_in filename in
Std.input_list chan
...这几乎就是您所拥有的.
...which is pretty much what you have.
如果您有包含电池的库,则可以将文件读取到Enum.t中,然后对其进行如下迭代:
If you have the Batteries-included library you could read a file into an Enum.t and iterate over it as follows:
let filelines = File.lines_of filename in
Enum.iter ( fun line -> (*Do something with line here*) ) filelines
这篇关于如何从OCaml的文本文件中读取行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文