How do I read in a text file and print them out to a file in Prolog?

拜拜、爱过 提交于 2019-12-24 10:18:08

问题


I have a text file, and I want to read it in and print them out in screen and write them into a new output file. So what I have done so far is

main :-
    open('text.txt', read, ID),  % open a stream
    repeat,             % try again forever
    read(ID, X),        % read from the stream
    write(X), nl,       % write to current output stream
    X == end_of_file,   % fail (backtrack) if not end of 
    !,
    close(ID).

But I only received an error message like,

ERROR: text.txt:1:0: Syntax error: Operator expected

What should I do?


回答1:


read/2 reads valid Prolog text. The message suggests, that in line 1 of text.txt you have some invalid Prolog text. Maybe a couple of words separated by spaces.

If you want to read regular text, you can do it very low-level using get_char/2, or you might want to do it more high level using grammars. SWI-Prolog has library(pio) for that.

Here is the Prolog programmer's equivalent to grep -q.

?- phrase_from_file((...,"root",...),'/etc/passwd').
true ;
true ;
true ;
false.

Actually, that's rather grep -c.

You need to load following definition for it:

... --> [] | [_], ... .



回答2:


If you want a reusable snippet:

%%  file_atoms(File, Atom) is nondet.
%
%   read each line as atom on backtrack
%
file_atoms(File, Atom) :-
    open(File, read, Stream),
    repeat,
    read_line_to_codes(Stream, Codes),
    (   Codes \= end_of_file
    ->  atom_codes(Atom, Codes)
    ;   close(Stream), !, fail
    ).

This calls read_line_to_codes, a SWI-Prolog builtin.




回答3:


is_eof(FlHndl, CharCode, CurrentLine, FileAkku, FileContent) :-
        CharCode == -1,
        append(FileAkku, [CurrentLine], FileContent),
        close(FlHndl), !.

is_newline(FlHndl, CharCode, CurrentLine, FileAkku, FileContent) :-
        CharCode == 10,
        append(FileAkku, [CurrentLine], NextFileAkku),
        read_loop(FlHndl, '', NextFileAkku, FileContent).

append_char(FlHndl, CharCode, CurrentLine, FileAkku, FileContent) :-
        char_code(Char, CharCode),
        atom_concat(CurrentLine, Char, NextCurrentLine),
         read_loop(FlHndl, NextCurrentLine, FileAkku, FileContent).

read_file(FileName, FileContent) :-
        open(FileName, read, FlHndl),
        read_loop(FlHndl, '', [], FileContent), !.

read_loop(FlHndl, CurrentLine, FileAkku, FileContent) :-
        get_code(FlHndl, CharCode),
        ( is_eof(FlHndl, CharCode, CurrentLine, FileAkku, FileContent)
        ; is_newline(FlHndl, CharCode, CurrentLine, FileAkku, FileContent)
        ; append_char(FlHndl, CharCode, CurrentLine, FileAkku, FileContent)).

main(InputFile, OutputFile) :-
    open(OutputFile, write, OS),
    (   read_file(InputFile,InputLines),
        member(Line, InputLines),
        write(Line), nl,
        write(OS,Line),nl(OS),
        false
        ;
        close(OS)
    ).

So, you can use it like main('text.txt', 'output.txt').



来源:https://stackoverflow.com/questions/8108049/how-do-i-read-in-a-text-file-and-print-them-out-to-a-file-in-prolog

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!