【问题标题】:How to read a file in SML line by line如何逐行读取 SML 中的文件
【发布时间】:2021-05-21 06:10:28
【问题描述】:

我想编写一个逐行读取文本文件的函数。即,阅读第一行直到 EOL,然后将其视为字符串,并重复此操作直到文本结束。 到目前为止,我只管理了这么多:

fun read file = 
let val is = TextIO.openIn file
in
end

【问题讨论】:

    标签: sml smlnj


    【解决方案1】:

    您可以为此使用TextIO.inputLine : instream -> string option。使用refs 你可以做类似的事情

    let
      val is = TextIO.openIn file
      val done = ref false
    in
      while not (!done) do
        case TextIO.inputLine of
          SOME s => print s
        | NONE => done := true
    end
    

    您也可以使用TextIO.StreamIO中的函数。

    【讨论】:

      猜你喜欢
      • 2020-08-13
      • 2011-11-02
      • 1970-01-01
      • 2011-12-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多