Re: read from file

Vic Bancroft <[email protected]>
Newsgroups gmane.comp.gnu.prolog.general
Organization netizens
Message-ID <[email protected]>
[email protected] wrote:

> hi,
>  
>
Aloha

>i want to read data from the file (data.txt). and want to use these prolog predicate like
>a(book)
>a(computer)
>a(phone)
>a(pen)
>a(pencil)
>  
>
Unless you use a read option for end_of_term, your term data should have 
the 'dot' delimeter, e.g.,

    a(book).
    a("computer").
    a("phone").
    a('pen').
    a('pencil').

Also note the distinction between an list of characters (using the 
double quote) and an atom (using the single quote).  This is most easily 
seen using the writeq/1 predicate, as in,

    terms(A) :-
        see(A),
        repeat,
        read(B),
        writeq(B),
        nl,
        (   B = end_of_file ->
            true, !
        ;   fail
        ),
        seen.

This predicate results in the output,

  | ?- terms( 'term_data.txt' ).
  a(book)
  a([99,111,109,112,117,116,101,114])
  a([112,104,111,110,101])
  a(pen)
  a(pencil)
  end_of_file
 
  yes

Did you really need to use read_atom/1 or perhaps read_token/1 ?

more,
l8r,
v


-- 
"The future is here. It's just not evenly distributed yet."
 -- William Gibson, quoted by Whitfield Diffie
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.