Javatpoint Logo
Javatpoint Logo

File Input and Output in Prolog

File Output: Changing the Current Output Stream

  • The tell/1 predicate is used to change the current output stream. This predicate has a single argument as variable or atom, which represents the name of a file like tell('outfile.txt').
  • When tell goal is evaluated, due to this, the name of the file becomes the current output stream. The specified name file is first created if the file is not already open. If the existing file has the same name, it is deleted.
  • When we select a new current output stream, the file which corresponds to the previous current output stream remains open. Using the told predicate, we can close only the current output stream.
  • The user is the default current output stream, i.e., terminal of the user. Using either told(user) or told predicate, we can restore this value.
  • The told/0 predicate is a built-in predicate that has no argument. When the told goal is evaluated, it closes the current output file and resets the current output stream to the user, i.e., terminal of the user.
  • The telling/1 predicate is a built-in predicate that has one argument. This argument must be an unbound variable. When the telling goal is evaluated, due to this, the variable to be bound to the current output stream name.

Output to a File

The above definition of tell says that 'any existing file which has the same name is deleted'. Some application has another possibility like the file is not deleted, and any output is placed after the end of existing file contents. In any practical implementation of Prolog, 'append' and 'overwrite' options are available, but it will not use the tell predicate, it may involve using the different predicate.

File Input: Changing the current Input Stream

  • The see/1 predicate is used to change the current input stream. This predicate has a single argument as variable or atom, which represents the name of a file like see('myfile.txt').
  • When see goal is evaluated, due to this, the name of the file becomes the current input stream. The specified name file is first open(only for read access) if the file is not already open. An error will be generated if we are unable to open a given name of the file.
  • When we select a new input stream, the file which corresponds to the current input stream remains open. Using the seen predicate, we can only close the current input stream.
  • The user is the default current input stream, i.e., terminal of the user. Using either see(user) or seen/0 predicate, we can restore this value.
  • The seen/0 predicate is a built-in predicate that has no argument. When the see goal is evaluated, it closes the current input file and reset the current input stream to the user, i.e., terminal of the user.
  • The seeing/1 predicate is a built-in predicate that has one argument. This argument must be an unbound variable. When the seeing goal is evaluated, due to this, the variable to be bound to the current input stream name.

Reading from Files: End of Files

When we evaluate the read(A) goal, and if the end of file is encountered, variable A will be bound to atom end_of_file.

When we evaluate the get0(A) or get(A), and if the end of file is encountered, variable A will be bound to ASCII value. The range of ASCII values is 0 to 255. This will typically be -1, but on the implementation of Prolog, it may vary one to another.

Reading from Files: End of Record

On the basis of the Prolog version, there may be incompatibility for the input of character between reading the end of record from a file and from a terminal of the user.

At the user's terminal, the end of a line of input will be indicated using the character with 13 ASCII value. In a file, the end of record will generally be indicated using the two ASCII values that are 13 followed by 10.

The below program reads a series of characters from the keyboard and prints that characters one per line.

Instead of write, we use put that test for ASCII value 13. In this, we don't need to not use the * character to indicate 'end of input'.


Next TopicExamples of Files




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA