login

Availability::- use_module(library(pure_input)).(can be autoloaded)
[nondet]phrase_from_file(:Grammar, +File)
Process the content of File using the DCG rule Grammar. The space usage of this mechanism depends on the length of the not committed part of Grammar. Committed parts of the temporary list are reclaimed by the garbage collector, while the list is extended on demand. Here is a very simple definition for searching a string in a file:
... --> []|[_],... .

file_contains(File, Pattern) :-
        phrase_from_file((..., Pattern, ...), File).

match_count(File, Pattern, Count) :-
        findall(x, file_contains(File, Pattern), Xs),
        length(Xs, Count).

This can be called as (note that the pattern must be a string (code list)):

?- match_count('pure_input.pl', "file", Count).