Prolog Operators

Prolog Lists

Built-In Predicates

Miscellaneous

Prolog - Reading and Writing Files



In this chapter, we're seeing how we can use files to read from, and write into the files. There are some built-in predicates, that can be used to read from file and write into it.

The tell and told Predicates

If we want to write into a file, except the console, we can write the tell() predicate. This tell()predicate takes filename as argument. If that file is not present, then it will create a new file, and write into it. That file will be opened until we write the told command. We can open more than one file using tell(). When told is called, all files will be closed.

Prolog Commands

| ?- told('myFile.txt').
uncaught exception: error(existence_error(procedure,told/1),top_level/0)
| ?- tell('myFile.txt').

yes
| ?- write('Hello World').

yes
| ?- write(' Writing into a file'),tab(5),write('myFile.txt'),nl.

yes
| ?- write("Write some ASCII values").

yes
| ?- told.

yes
| ?- 

Output (myFile.txt)

Hello World Writing into a file     myFile.txt
[87,114,105,116,101,32,115,111,109,101,32,65,83,67,73,73,32,118,97,108,117,101,115]

Similarly, we can also read from files. Let us see some example of reading from file.

The see and seen predicates

When we want to read from file, not from the keyboard, we have to change current input stream. So we can use see() predicate. This will take filename as input. When the read operation is completed, then we will use seen command.

Sample File (sample_predicate.txt)

likes(lili, cat).
likes(jhon,dog).

Output

| ?- see('sample_predicate.txt').

yes
| ?- read(X).

X = likes(lili,cat)

yes
| ?- read(Y).

Y = likes(jhon,dog)

(16 ms) yes
| ?- seen.

yes

So from this example, we can see that using the see() predicate we can read from the file. Now after using seen command, the control transfers to the console again. So finally it takes input from console.

Processing files of terms

We have seen how to read specific contents (few lines) of a file. Now if we want to read/process all the contents of a file, we need to write a clause to process file (process_file), until we reach the end of the file.

Program(process_file.pl)

process_file :- read(Line),   Line \== end_of_file, % when Line is not not end of file, call process.   
process(Line).
process_file :- !. % use cut to stop backtracking
process(Line):-  write(Line),nl,   process_file. %this will print the line into the console

Sample File (sample_predicate.txt)

likes(lili, cat).
likes(jhon,dog).
domestic(dog).
domestic(cat).

Output

| ?- consult('D:/TP Prolog/Sample Codes/process_file.pl').
compiling D:/TP Prolog/Sample Codes/process_file.pl for byte code...
D:/TP Prolog/Sample Codes/process_file.pl compiled, 3 lines read - 774 bytes written, 3 ms
warning: D:/TP Prolog/Sample Codes/process_file.pl:4: redefining procedure process/1
         D:/TP Prolog/Sample Codes/read_write.pl:2: previous definition

(16 ms) yes
| ?- see('sample_predicate.txt'), process_file, seen.
likes(lili,cat)
likes(jhon,dog)
domestic(dog)
domestic(cat)

true ? 

(47 ms) yes
| ?- 
prolog_inputs_and_outputs.htm
Advertisements