Prolog file IO:

File I/O
File I/O is fairly traditional - you open an input or output stream, specifying the name of the file, and then read from or write to the stream (closing the stream when you're done with the file).

There are two approaches, a simplified approach using tell and see, or a more hands-on approach using open, close, read, write

tell and see

tell('filename') specifies that all subsequent output is to be directed to that file.
telling(F) identifies which file we are currently writing to (user if stdout)
told. closes the filestream

Similarly, see('filename'), seeing(F), and seen identify which file to use as the source for input.

open and close

When opening files you specify the access mode (e.g. read, write, append).

EXAMPLES:


% readfile(Filename, Term)
%    allows us to read a term from a file,
%       if the filename passed to us is uninstantiated then we will first 
%          prompt the user for a filename
%       Term will be set to the atom end_of_file
%          if the end of file is read

% read a term from a file, where the user has not yet specified the filename
readfile(Filename,Term) :- var(Filename),                 % if they didn't give a filename
                           % prompt them to enter one IN SINGLE QUOTES
                           write('enter a filename in single quotes '), nl, 
                           read(Filename),                % read the filename
                           readfile(Filename, Term).      % call the other readfile rule.

% read a term from a specified file
readfile(Filename,Term) :- nonvar(Filename),              % if they already specified a filename
                           exists_file(Filename),         % make sure the file exists
                           access_file(Filename, read),   % make sure we have read permission
                           open(Filename, read, Stream),  % open the file for reading
                           read(Stream, Term),            % read the first term
                           close(Stream),                 % close the file
                           write('The first term in '),   % give an output message
                           write(Filename), 
                           write(' is:'), nl, 
                           write(Term), nl.                % echo the contents

% if neither of the above methods worked, then display an error message
readfile :- write('Unable to perform the requested read'), nl.


% writetest allows us to write a term to a file,
%    if the filename passed to us is uninstantiated then we will first 
%    prompt the user for a filename

% write some content to a file, where the user has not yet supplied the filename
writefile(Filename, Content) :- var(Filename),              % if they didn't give a filename
                             % prompt them to enter one IN SINGLE QUOTES
                             write('enter a filename in single quotes '), nl, 
                             read(Filename),                % read the filename
                             writefile(Filename, Content).  % call the other readtest rule.

writefile(Filename, Content) :- nonvar(Filename),           % if a filename was supplied
                             open(Filename, write, Stream), % open the file
                             write(Stream, Content),        % write the content
                             nl(Stream),                    % write a newline
                             close(Stream).                 % close the file

% if neither of the above methods worked, then display an error message
writefile :- write('Unable to perform the requested write'), nl.

File system interaction

There is a substantial collection of methods to interact with the filesystem: