A The SWI-Prolog library
AllApplicationManualNameSummaryHelp

  • Documentation
    • Reference manual
      • The SWI-Prolog library
        • library(aggregate): Aggregation operators on backtrackable predicates
        • library(ansi_term): Print decorated text to ANSI consoles
        • library(apply): Apply predicates on a list
        • library(assoc): Association lists
        • library(broadcast): Broadcast and receive event notifications
        • library(charsio): I/O on Lists of Character Codes
        • library(check): Consistency checking
        • library(clpb): CLP(B): Constraint Logic Programming over Boolean Variables
        • library(clpfd): CLP(FD): Constraint Logic Programming over Finite Domains
        • library(clpqr): Constraint Logic Programming over Rationals and Reals
        • library(csv): Process CSV (Comma-Separated Values) data
        • library(dcg/basics): Various general DCG utilities
        • library(dcg/high_order): High order grammar operations
        • library(debug): Print debug messages and test assertions
        • library(dicts): Dict utilities
        • library(error): Error generating support
        • library(gensym): Generate unique identifiers
        • library(iostream): Utilities to deal with streams
        • library(lists): List Manipulation
        • library(main): Provide entry point for scripts
        • library(nb_set): Non-backtrackable set
        • library(www_browser): Activating your Web-browser
        • library(occurs): Finding and counting sub-terms
        • library(option): Option list processing
        • library(optparse): command line parsing
        • library(ordsets): Ordered set manipulation
        • library(pairs): Operations on key-value lists
        • library(persistency): Provide persistent dynamic predicates
        • library(pio): Pure I/O
        • library(predicate_options): Declare option-processing of predicates
        • library(prolog_pack): A package manager for Prolog
        • library(prolog_xref): Cross-reference data collection library
        • library(quasi_quotations): Define Quasi Quotation syntax
        • library(random): Random numbers
        • library(readutil): Reading lines, streams and files
          • read_line_to_codes/2
          • read_line_to_codes/3
          • read_stream_to_codes/2
          • read_stream_to_codes/3
          • read_file_to_codes/3
          • read_file_to_terms/3
        • library(record): Access named fields in a term
        • library(registry): Manipulating the Windows registry
        • library(settings): Setting management
        • library(simplex): Solve linear programming problems
        • library(solution_sequences): Modify solution sequences
        • library(thread_pool): Resource bounded thread management
        • library(ugraphs): Unweighted Graphs
        • library(url): Analysing and constructing URL
        • library(varnumbers): Utilities for numbered terms
        • library(yall): Lambda expressions
    • Packages

A.35 library(readutil): Reading lines, streams and files

This library contains primitives to read lines, files, multiple terms, etc. The package clib provides a shared object (DLL) named readutil. If the library can locate this shared object it will use the foreign implementation for reading character codes. Otherwise it will use a Prolog implementation. Distributed applications should make sure to deliver the readutil shared object if performance of these predicates is critical.

read_line_to_codes(+Stream, -Codes)
Read the next line of input from Stream and unify the result with Codes after the line has been read. A line is ended by a newline character or end-of-file. Unlike read_line_to_codes/3, this predicate removes a trailing newline character.

On end-of-file the atom end_of_file is returned. See also at_end_of_stream/[0,1].

read_line_to_codes(+Stream, -Codes, ?Tail)
Difference-list version to read an input line to a list of character codes. Reading stops at the newline or end-of-file character, but unlike read_line_to_codes/2, the newline is retained in the output. This predicate is especially useful for reading a block of lines up to some delimiter. The following example reads an HTTP header ended by a blank line:
read_header_data(Stream, Header) :-
        read_line_to_codes(Stream, Header, Tail),
        read_header_data(Header, Stream, Tail).

read_header_data("\r\n", _, _) :- !.
read_header_data("\n", _, _) :- !.
read_header_data("", _, _) :- !.
read_header_data(_, Stream, Tail) :-
        read_line_to_codes(Stream, Tail, NewTail),
        read_header_data(Tail, Stream, NewTail).
read_stream_to_codes(+Stream, -Codes)
Read all input until end-of-file and unify the result to Codes.
read_stream_to_codes(+Stream, -Codes, ?Tail)
Difference-list version of read_stream_to_codes/2.
read_file_to_codes(+Spec, -Codes, +Options)
Read a file to a list of character codes. Spec is a file specification for absolute_file_name/3. Codes is the resulting code list. Options is a list of options for absolute_file_name/3 and open/4. In addition, the option tail(Tail) is defined, forming a difference-list.
read_file_to_terms(+Spec, -Terms, +Options)
Read a file to a list of Prolog terms (see read/1). Spec is a file specification for absolute_file_name/3. Terms is the resulting list of Prolog terms. Options is a list of options for absolute_file_name/3 and open/4. In addition, the option tail(Tail) is defined, forming a difference-list.