?- open_string("Hello@",Input), read_string(Input, ",@", "\t ", End, String) Input = <stream>(0x1ca11d0), End = 41, String = "Hello".
Reading user input:
?- read_string(user_input, "\n", "\t ", End, String). |: Hello End = 10, String = "Hello".
Did you know ... | Search Documentation: |
Predicate read_string/5 |
The predicate read_string/5 called repeatedly on an input until Sep is -1 (end of file) is equivalent to reading the entire file into a string and calling split_string/4, provided that SepChars and PadChars are not partially overlapping.175Behaviour that is fully compatible would require unlimited look-ahead. Below are some examples:
Read a line:
read_string(Input, "\n", "\r", Sep, String)
Read a line, stripping leading and trailing white space:
read_string(Input, "\n", "\r\t ", Sep, String)
Read up to‘,
’or‘)
’,
unifying Sep with 0',
i.e. Unicode 44, or 0')
,
i.e. Unicode 41:
read_string(Input, ",)", "\t ", Sep, String)
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.
?- open_string("Hello@",Input), read_string(Input, ",@", "\t ", End, String) Input = <stream>(0x1ca11d0), End = 41, String = "Hello".
Reading user input:
?- read_string(user_input, "\n", "\t ", End, String). |: Hello End = 10, String = "Hello".