Did you know ... | Search Documentation: |
![]() | Predicate open/3 |
file_lines(File, Lines) :- setup_call_cleanup(open(File, read, In), stream_lines(In, Lines), close(In)). stream_lines(In, Lines) :- read_string(In, _, Str), split_string(Str, "\n", "", Lines).
This loads all lines to memory as strings. If we use the Unix dictionary words:
?- file_lines("/usr/share/dict/words", Lines). Words = ["A", "a", "aa", "aal", "aalii", "aam", "Aani", "aardvark", "aardwolf"|...].
file_line(File, Line) :- setup_call_cleanup(open(File, read, In), stream_line(In, Line), close(In)). stream_line(In, Line) :- repeat, ( read_line_to_string(In, Line0), Line0 \== end_of_file -> Line0 = Line ; !, fail ).
This will backtrack over consecutive lines in the input. If we use the Unix dictionary words:
?- file_line("/usr/share/dict/words", Word). Word = "A" ; Word = "a" ; Word = "aa" ; Word = "aal" ; % and so on
This doesn't load all lines to memory, so it can be used with very large files.