Next: How to use DCGs
Up: Input/Output
Previous: Input/Output
  Contents
  Index
Reading input data into data structures
The easiest way to read data into ECLiPSe programs is to use the Prolog term format for the data. Each term is terminated by a fullstop, which is a dot (.) followed by some white space. The following code reads terms from a file until the end of file is encountered and returns the terms in a list.
:-mode read_data(++,-).
read_data(File,Result):-
open(File,read,S),
read(S,X),
read_data_lp(S,X,Result),
close(S).
read_data_lp(_S,end_of_file,[]):-
!.
read_data_lp(S,X,[X|R]):-
read(S,Y),
read_data_lp(S,Y,R).
This method is very easy to use if both source and sink of the data are ECLiPSe programs. Unfortunately, data provided by other applications will normally not be in the Prolog term format. For them we will have to use some other techniques6.1.
Warwick Harvey
2004-08-07