For Programmers: Free Programming Magazines  


Home > Archive > Prolog > September 2006 > read_from_chars/2









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author read_from_chars/2
Mika

2006-09-14, 7:01 pm

hello,
I do not succeed to find the predicate of SwiProlog equivalent to
predicate read_from_chars/2 of SicstusProlog. Someone can help me.
Thanks.

Jan Wielemaker

2006-09-14, 7:01 pm

On 2006-09-14, Mika <miky821@alice.it> 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

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com