
- Prolog - Home
- Prolog - Introduction
- Prolog - Environment Setup
- Prolog - Hello World
- Prolog - Basics
- Prolog - Relations
- Prolog - Data Objects
- Loop & Decision Making
- Conjunctions & Disjunctions
Prolog Operators
- Prolog - Type of Operators
- Prolog - Arithmetic Comparison Operators
- Prolog - Unification Operators
- Prolog - Term Comparision Operators
- Prolog - Arithmetic Operators
- Prolog - Logical Operators
- Prolog - List Operators
- Prolog - Custom Operators
Prolog Lists
- Prolog - Lists
- Prolog - Member of List
- Prolog - Length of List
- Prolog - Concatenating Lists
- Prolog - Appending to a List
- Prolog - Deleting from a List
- Prolog - Inserting into a List
- Prolog - Permutation Operation
- Prolog - Combination Operation
- Prolog - Reverse Items of a List
- Prolog - Shift Items of a List
- Prolog - Check Order of a List
- Prolog - SubSet of a Set
- Prolog - Union of Sets
- Prolog - Intersection of Sets
- Prolog - Even and Odd Length Finding
- Prolog - Divide a List
- Prolog - Find Maximum of a List
- Prolog - Find Minimum of a List
- Prolog - Find Sum of a List
- Prolog - Sorting List using MergeSort
Built-In Predicates
- Prolog - Built-In Predicates
- Prolog - Identifying Terms
- Prolog - Decomposing Structures
- Prolog - Collecting All
- Prolog - Mathematical Predicates
- Prolog - Scientific Predicates
Miscellaneous
- Recursion and Structures
- Prolog - Backtracking
- Prolog - Preventing Backtracking
- Prolog - Different and Not
- Prolog - Inputs and Outputs
- Tree Data Structure (Case Study)
- Prolog - Examples
- Prolog - Basic Programs
- Prolog - Practical Arithmetic Examples
- Prolog - Examples of Cuts
- Towers of Hanoi Problem
- Prolog - Linked Lists
- Monkey and Banana Problem
- Prolog Useful Resources
- Prolog - Quick Guide
- Prolog - Useful Resources
- Prolog - Discussion
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 | ?-