如何读入文本文件并将其打印到 Prolog 中的文件中?

发布于 2024-12-15 01:57:37 字数 484 浏览 2 评论 0原文

我有一个文本文件,我想读入它并在屏幕上打印它们并将它们写入新的输出文件。所以到目前为止我所做的是

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).

但我只收到一条错误消息,例如

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

我应该做什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

永不分离 2024-12-22 01:57:37

read/2 读取有效的 Prolog 文本。该消息表明,在 text.txt 的第 1 行中,您有一些无效的 Prolog 文本。也许是几个用空格分隔的单词。

如果您想阅读常规文本,您可以使用 get_char/2 进行非常低级的操作,或者您可能想使用语法进行更高级别的操作。 SWI-Prolog 有用于此目的的library(pio)

这是 Prolog 程序员相当于 grep -q 的命令。

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

实际上,这更像是 grep -c

您需要为其加载以下定义:

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

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:

... --> [] | [_], ... .
慕烟庭风 2024-12-22 01:57:37

如果您想要一个可重用的代码片段:

%%  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
    ).

这将调用 read_line_to_codes,一个 SWI-Prolog 内置函数。

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.

浅浅 2024-12-22 01:57:37
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)
    ).

因此,您可以像 main('text.txt', 'output.txt') 一样使用它。

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').

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文