【问题标题】:SML/NJ Read next character from file ignoring whitespaceSML/NJ 从文件中读取下一个字符,忽略空格
【发布时间】:2014-10-04 17:02:57
【问题描述】:

你能给我一个解决方案来创建一个函数来读取文件流中的下一个字符,忽略空格吗? (在 SML/NJ)。

【问题讨论】:

    标签: input character smlnj


    【解决方案1】:

    您需要使用以下库:

    Char 满足您的空白需求。

    Text_IO 满足您的流媒体需求。

    下面的代码是一个例子,这不是我最好的代码:)

    let 
        (* Use TextIO library to open instream to file *)
        val ins = TextIO.openIn("C:\\Test.txt")
    
        (* This function takes the instream, and repeats 
            calling itself untill we are the end of the file *)
        fun handleStream (ins : TextIO.instream) =
            let 
                (* Read one character, hence the name, input1*)
                val character = TextIO.input1 (ins)
    
                (* This function decides whether to close the stream
                    or continue reading, using the handleStream function 
                    recursively.
                *)
                fun helper (copt : char option) =
                    case copt of 
                        (* We reached the end of the file*)
                        NONE => TextIO.closeIn(ins)
                        (* There is something! *)
                      | SOME(c) => 
                            ((if (Char.isSpace c) 
                                then
                                    (*ignore the space*)
                                    ()
                                else
                                    (* Handle the character c here *)
                                    ())
                            (* Recursive call *)
                            ; handleStream(ins))
            in
                (* start the recursion *)
                helper (character)
            end
    in
        (* start handling the stream *)
        handleStream( ins )
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多