Re: read_from_chars/2



On 2006-09-14, Mika <miky821@xxxxxxxx> wrote:
hello,
I do not succeed to find the predicate of SwiProlog equivalent to
predicate read_from_chars/2 of SicstusProlog. Someone can help me.

In most cases atom_to_term/3 will do the trick. If not, there is the
library memfile. Here is the code.

with_input_from_chars(Chars, Goal) :-
new_memory_file(MemFile),
open_memory_file(MemFile, write, Out),
format(Out, '~s', [Chars]),
close(Out),
open_memory_file(MemFile, read, In),
current_input(OldIn),
set_input(In),
call_cleanup(Goal,
( close(In),
set_input(OldIn),
free_memory_file(MemFile))).

1 ?- with_input_from_chars("hello. ", read(X)).

X = hello

Enjoy --- Jan

.