I want to read a file that will contain names in each line. And I don't know how to do this with SWI Prolog. I have sufficient knowledge of C/C++ so is there any way to do the same with Prolog?
Asked
Active
Viewed 1,529 times
1
-
1to open files check this http://www.swi-prolog.org/pldoc/doc_for?object=section%282,%274.15%27,swi%28%27/doc/Manual/IO.html%27%29%29, to actually read the data check mat's answer. there is also this http://www.swi-prolog.org/pldoc/doc_for?object=section%282,%27A.23%27,swi%28%27/doc/Manual/readutil.html%27%29%29 – Thanos Tintinidis Nov 29 '11 at 15:55
1 Answers
3
In my opinion the best way to read from files in SWI Prolog (especially if you want to do something that is not covered by standard predicates such as read/2 which reads Prolog terms from streams) is to first write a DCG (= definite clause grammar) to declaratively specify what you want to describe. You can use the DCG interactively in queries to try it out (i.e., parse and often also generate the strings it describes). Then, use SWI-Prolog's library(pio) to apply the DCG to a file with phrase_from_file/2.

mat
- 40,498
- 3
- 51
- 78
-
See also [this closed thread](http://stackoverflow.com/questions/8231754/reading-a-file-in-prolog) – false Nov 29 '11 at 15:55