Reputation: 41
I am writing a program in SMLNJ which should open a txt file and then create a list with each character from this file. So I did this:
val charlist = explode(TextIO.input(TextIO.openIn "myfile.txt"))
However, for one specific input (not the largest one of my test cases) it stops parsing after some characters. After, looking into it I tried replacing TextIO.input
with TextIO.inputAll
and now it works! The thing is I do not understand why, so googled it and found this:
[input istr] reads some elements from istr, returning a vector v of those elements. The vector will be empty (size v = 0) if and only if istr is at end of stream or is closed. May block (not return until data are available in the external world).
[inputAll istr] reads and returns the string v of all characters remaining in istr up to end of stream.
But, I am still confused over the difference between these two. What's the criterion for how many elements will be read in the first case? And what does "May block (not return until data are available in the external world)" mean?
Upvotes: 0
Views: 55
Reputation: 36620
If we call TextIO.input
on a text file, it will read the whole file. However, if we call it on TextIO.stdIn
, it will read the first line input and return it. Note that the documentation you quoted does say that it will read some elements and not until the end of the stream.
However, TextIO.inputAll
does read until the end of the stream, and testing this on standard input will make this clear.
Blocking means that the program will not continue until input is available. If you evaluate TextIO.input TextIO.stdIn
your program will not continue until data is available or the end of the stream is reached.
Upvotes: 0