Home » Simple Input-Output in Prolog

Simple Input-Output in Prolog

by Online Tutorials Library

Simple Input-Output in Prolog

We can read the data using the following predicates of Prolog:

To provide the input source, File= ‘user ‘ will choose the keyboard. Otherwise, File should be the existing file disk name. The following program shows a program to read the file data and print it to the screen as follows:

We can issue the following goal to inspect the source file for ‘browse’. Assume that name of browse is browse.pro:

When ‘seeing(Old)’ is satisfied by the Prolog, the variable Old will bound to the port and that port is the currently coming read-inputs. When ‘see(File)’ is satisfied by the Prolog, the file will open whose name is bound as value to ‘File’ and stream or read port is associated with the file.

In the program, the ‘Data’ variable should take value. The sequence of numbers or a Prolog program can contain by the ‘File’. In the following section, a file of numbers will be formatted as follows:

The following example shows the interactive version of the program of browser:

Here we will show how to use this version to pay attention to the form that the user supplies the filename.

In Prolog, to write the data, we can use the following predicates:

We will open the output destination screen using the File = ‘User’. The following Prolog program is used to save the current program in memory to a file.

When ‘telling(Old)’ is satisfied by the Prolog, the variable ‘Old’ will bound to the port and that port is associated with the current destination of write-output. When ‘tell(ToFile)’ is satisfied by the Prolog, the name of the file bound to ‘ToFile’ will be opened, and stream or output port will be associated with the file. Using the stream or port, the subsequent writing will go to that file. Then ‘told’ will close the file.


Next Topic#

You may also like