How do I read in lines from a text file in OCaml?

后端 未结 7 935
心在旅途
心在旅途 2020-12-31 02:18

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         


        
相关标签:
7条回答
  • 2020-12-31 03:11

    Here is a simple recursive solution that does not accumulate the lines or use external libraries, but lets you read a line, process it using a function, read the next recursively until done, then exit cleanly. The exit function closes the open filehandle and signals success to the calling program.

    let read_lines file process =
      let in_ch = open_in file in
      let rec read_line () =
        let line = try input_line in_ch with End_of_file -> exit 0
        in (* process line in this block, then read the next line *)
           process line;
           read_line ();
    in read_line ();;
    
    read_lines some_file print_endline;;
    
    0 讨论(0)
提交回复
热议问题