4.36 File System Interaction
AllApplicationManualNameSummaryHelp

  • Documentation
    • Reference manual
      • Built-in Predicates
        • File System Interaction
          • access_file/2
          • exists_file/1
          • file_directory_name/2
          • file_base_name/2
          • same_file/2
          • exists_directory/1
          • delete_file/1
          • rename_file/2
          • size_file/2
          • time_file/2
          • absolute_file_name/2
          • absolute_file_name/3
          • is_absolute_file_name/1
          • file_name_extension/3
          • directory_files/2
          • expand_file_name/2
          • prolog_to_os_filename/2
          • read_link/3
          • tmp_file/2
          • tmp_file_stream/3
          • make_directory/1
          • delete_directory/1
          • working_directory/2
          • chdir/1
    • Packages
Availability:built-in
Sourcetmp_file_stream(+Encoding, -FileName, -Stream)
tmp_file_stream(-FileName, -Stream, +Options)
Create a temporary filename FileName, open it for writing and unify Stream with the output stream. If the OS supports it, the created file is only accessible to the current user and the file is created using the open()-flag O_EXCL, which guarantees that the file did not exist before this call. The following options are processed:
encoding(+Encoding)
Encoding of Stream. Default is the value of the Prolog flag encoding. The value binary opens the file in binary mode.
extension(+Ext)
Ensure the created file has the given extension. Default is no extension. Using an extension may be necessary to run external programs on the file.

This predicate is a safe replacement of tmp_file/2. Note that in those cases where the temporary file is needed to store output from an external command, the file must be closed first. E.g., the following downloads a file from a URL to a temporary file and opens the file for reading (on Unix systems you can delete the file for cleanup after opening it for reading):

open_url(URL, In) :-
        tmp_file_stream(text, File, Stream),
        close(Stream),
        process_create(curl, ['-o', File, URL], []),
        open(File, read, In),
        delete_file(File).              % Unix-only

Temporary files created using this call are removed if the Prolog process terminates gracefully. Calling delete_file/1 using FileName removes the file and removes the entry from the administration of files-to-be-deleted.