Previous Up Next

Chapter 5  File I/O

The FileIO module provides simple access to reading and writing files. This module is defined in the ``file-io'' library. It has the following signature:
type InFile
type OutFile

val openIn : (String) -> Option(InFile)
val openOut : (String) -> Option(OutFile)
val appendOut : (String) -> Option(OutFile)
val closeIn : (InFile) -> ()
val closeOut : (OutFile) -> ()
val print : (OutFile, String) -> ()
val putc : (OutFile, Char) -> ()
val inputLine : (InFile) -> String
val getc : InFile -> Option(Char)

Note that because of a bug in the current implementation of the gen-mbi tool, the InFile and OutFile types are pervasive (i.e., they are not qualified by FileIO).

The file I/O operations have the following semantics:
InFile

This is the type of an input file.
OutFile

This is the type of an output file.
openIn

This function opens the file specified by its argument for reading. If the file cannot be opened, then None is returned.
openOut

This function opens the file specified by its argument for writing. If the file already exists, it is truncated. If the file cannot be opened, then None is returned.
appendOut

This function opens the file specified by its argument for writing. If the file already exists, optput will be appended to it. If the file cannot be opened, then None is returned.
closeIn

This function closes the input file given as its argument.
closeOut

This function closes the output file given as its argument.
print

This function prints its argument to the given output file.
putc

This function prints its argument to the given output file.
inputLine

This function reads a line of input from the given input file. The empty string is returned on end-of-file.
getc

This function reads a single character of input from the given input file. The value None is returned on end-of-file.

Previous Up Next