Re: [eclipse-clp-users] Beginner question: how do I read from an open stream?

From: Joachim Schimpf <jschimpf_at_coninfer.com>
Date: Thu, 25 Jul 2013 20:12:46 +0200
On 25/07/2013 19:25, Volkan Unsal wrote:
> I have a SWI Prolog function that is not working in Eclipse, and I can't find documentation for
> reading the contents of a file. Here is my current approach (that is not working):
>
>
>     read_until_stop(File, [L|Lines]) :-
>          read_line_to_codes(File, Codes),
>          Codes \= end_of_file,
>          atom_codes(L, Codes),
>          L \= stop,
>          !,
>          read_until_stop(File, Lines).
>     read_until_stop(_, []).

Use read_string/4:

     read_until_stop(Stream, [L|Lines]) :-
         read_string(Stream, end_of_line, _Length, String),
         String \= "stop",
         !,
         atom_string(L, String),
         read_until_stop(Stream, Lines).
     read_until_stop(_, []).

This code will (like yours) return the Lines as atoms.  In ECLiPSe
it would normally be more efficient to work directly with the strings
returned by read_string/4 (omitting the atom_string conversion) - but
that requires changes to the rest of your code.


-- Joachim
Received on Thu Jul 25 2013 - 18:12:57 CEST

This archive was generated by hypermail 2.2.0 : Sat Jul 27 2013 - 06:16:48 CEST