

So from this example, we can see that using the see() predicate we can read from the file. Output | ?- see('sample_predicate.txt'),read(X),read(Y),seen,read(Z).the_end.X = end_of_fileY = end_of_fileZ = the_endyes| ?. Sample File (sample_predicate.txt) likes(lili, cat).likes(jhon,dog). When the read operation is completed, then we will use seen command. When we want to read from file, not from the keyboard, we have to change current input stream. Let us see some example of reading from file. Prolog Commands | ?- told('myFile.txt').uncaught exception: error(existence_error(procedure,told/1),top_level/0)| ?- told("myFile.txt").uncaught exception: error(existence_error(procedure,told/1),top_level/0)| ?- tell('myFile.txt').yes| ?- 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

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

This tell()predicate takes filename as argument. If we want to write into a file, except the console, we can write the tell() predicate. There are some built-in predicates, that can be used to read from file and write into it. In this section, we will see how we can use files to read from, and write into the files. Program | ?- write('hello'),tab(15),write('world').hello worldyes| ?- write('We'),tab(5),write('will'),tab(5),write('use'),tab(5),write('tabs').We will use tabsyes| ?- Reading/Writing Files So it takes a number as an argument, and prints those many number of blank spaces. The tab() is one additional predicate that can be used to put some blank-spaces while we write something. compiling D:/TP Prolog/Sample_Codes/read_ for byte code.D:/TP Prolog/Sample_Codes/read_ compiled, 9 lines read - 1226 bytes written, 12 ms(15 ms) yes| ?- cube.Write a number: 2.Cube of 2: 8Write a number: 10.Cube of 10: 1000Write a number: 12.Cube of 12: 1728Write a number: 8.Cube of 8: 512Write a number: stop.(31 ms) yes| ?- The tab() Predicate Program cube :- write('Write a number: '), read(Number), process(Number).process(stop) :- !.process(Number) :- C is Number * Number * Number, write('Cube of '),write(Number),write(': '),write(C),nl, cube. Now let us see one example to see how read() works. The read() is generally used to read from console, but this can also be used to read from files. User can write something in the console, that can be taken as input and process it. The read() predicate is used to read from console. But if we use double quote (“string”), then it will return a list of ASCII values. And from this example, it is clear that, if we want to print some string on the console, we have to use single quotes (‘string‘). Program | ?- write(56).56yes| ?- write('hello').helloyes| ?- write('hello'),nl,write('world').helloworldyes| ?- write("ABCDE").yesįrom the above example, we can see that the write() predicate can write the contents into the console. Let us see some examples of write() function. This predicate takes the parameter as input, and writes the content into the console by default. To write the output we can use the write() predicate. So this will be the input and output handling techniques. So here we will see that writing and reading tasks in more detail using prolog. In some cases, we print something on the console, that are written in our prolog code. So far we have seen that we can write a program and the query on the console to execute. Using some external file to read lines and termsĬharacter manipulation for input and outputĬonsulting prolog files into other prolog program techniques. We will use some built in predicates to do these tasks, and also see file handling techniques.įollowing topics will be discussed in detail −

In this chapter, we will see some techniques to handle inputs and outputs through prolog.
