fileio.sml 659 B

12345678910111213141516171819202122232425262728
  1. fun read file =
  2. let
  3. val inStream = TextIO.openIn file
  4. in
  5. (* TextIO.inputAll returns a TextIO.vector, which is a string. *)
  6. TextIO.inputAll inStream
  7. end;
  8. val str = read "example-file.txt";
  9. val lines = String.tokens (fn c => c = #"\n") str;
  10. lines;
  11. fun read_lines file =
  12. let
  13. val inStream = TextIO.openIn file
  14. fun iter inputStream =
  15. case TextIO.inputLine inputStream of
  16. SOME line => line :: iter inputStream
  17. | NONE => []
  18. val lines = iter inStream
  19. in
  20. TextIO.closeIn inStream;
  21. lines
  22. end;
  23. val lines = read_lines "example-file.txt";