Question

I'm trying to read text from a file in SML but I can't get it to work. Here's what I'm trying

fun read file =
let val inStream = TextIO.openIn file

in
        TextIO.StreamIO.input1 inStream
end

The actual function call input1 is not important, all I want is to be able to read from a file.

Was it helpful?

Solution

You error is in TextIO.StreamIO.input1, you most likely mean TextIO.input1.

If you really need/wan't to work with the file using StreamIO, you have to convert the instream type returned by openIN, to an StreamIO.instream with the TextIO.getInstream function.

You can read more about all this in the SML Basis library TEXT_IO signature.

Remember that it is good practice to close the files when you are done reading from them.

Update

As suggested in the comments, you can either read in the entire file (if you know it is small) or you can read it line by line.
The easiest thing to do, if you wan't to get the contents word by word, is to split the content by whitespace. This way you will end up with a list of strings that represent each individual "words" from the file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top