简体   繁体   中英

How to read a stream in Eclipse CLP?

I am new to Eclipse, and I have been trying to read a file stream without success. The code that I normally use for this in SWI-Prolog is this:

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(_, []).

But read_line_to_codes is not available in Eclipse apparently. What is a good alternative for this?

As suggested by the Eclipse manual the Eclipse counterpart would be

read_line(Stream, String) :-
    read_string(Stream, end_of_line, _Length, String).

with the difference that read_string returns the actual string as opposed to the list of codes, ie, atom_codes is no longer necessary:

?- read_string(input, end_of_line, Length, String).
      abcdefghi
      Length = 9
      String = "abcdefghi"
      yes.

I think read_line_to_codes/2 can easily be implemented in ECLiPSe, but for efficiency reuse available builtins. You can do with read_line /2.

Try to define

:- use_module(library(util)).
read_line_to_codes(S, L) :- read_line(S, L).

or simply call read_line...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM