GIML: How to use files
Reading programs
The use command takes a file name and parses and executes the contents as an
SML program.
The use instruction may of course appear in these files. Comments are enclosed in (* *)
pairs.
Reading data
The following are available (among other)
open_in | string -> instream | Create an input stream |
input_line | instream -> string | Get one line |
end_of_stream | instream -> boolean | Check for more |
can_input | instream -> int | Check for more |
input | instream * int -> string | Get loads |
close_in | instream -> unit | |
Handling input & output efficiently can be tricky. So don't bother, use one of
the following to load an entire file into either a single string or a list of
strings - let the operating system do the work.
fun fileToString fileName = let
val stream = open_in fileName
val str = input(stream, can_input stream)
val _ = close_in stream
in str end;
fun fileToList fileName = let
fun f fh = if end_of_stream fh then
let val _ = close_in fh in nil end
else (input_line fh)::(f fh)
in f(open_in fileName) end;
Writing data
This is very similar you have
- open_out : string -> outstream
- open_append : string -> outstream
- output : outstream * string -> unit
- flush_out : outstream -> unit
- close_out : outstream -> unit
A list of the IO structure
exception Io = Io
val std_in = - : instream
val std_out = - : outstream
val std_err = - : outstream
val open_in = fn : string -> instream
val open_out = fn : string -> outstream
val open_append = fn : string -> outstream
val open_string = fn : string -> instream
val close_in = fn : instream -> unit
val close_out = fn : outstream -> unit
val output = fn : outstream * string -> unit
val outputc = fn : outstream -> string -> unit
val input = fn : instream * int -> string
val inputc = fn : instream -> int -> string
val input_line = fn : instream -> string
val lookahead = fn : instream -> string
val end_of_stream = fn : instream -> bool
val can_input = fn : instream -> int
val flush_out = fn : outstream -> unit
val is_term_in = fn : instream -> bool
val is_term_out = fn : outstream -> bool
val set_term_in = fn : instream * bool -> unit
val set_term_out = fn : outstream * bool -> unit
val execute = fn : string * string list -> instream * outstream
val execute_in_env = fn
: string * string list * string list -> instream * outstream
val exportML = fn : string -> bool
val exportFn = fn : string * (string list * string list -> unit) -> unit