简体   繁体   English

从 prolog 中的文本文件中读取

[英]Reading from textfile in prolog

I have some trouble with reading from text files in GNU Prolog.我在读取 GNU Prolog 中的文本文件时遇到了一些麻烦。 I want to read a.txt file and move the file to a list.我想读取 a.txt 文件并将文件移动到列表中。 I have tried to follow some previous examples on stackoverflow to read the file, but have not been able to access the file (it seems).我试图按照 stackoverflow 上的一些先前示例来读取文件,但无法访问该文件(似乎)。 Here is what I've got so far:这是我到目前为止所得到的:

readFile:-
    open('text.txt', read, File),
    read_lines(File, Lines),
    close(File),
    write(Lines), nl.

read_lines(File,[]):- 
    at_end_of_stream(File).

read_lines(File,[X|L]):-
    \+ at_end_of_stream(File),
    read(File,X),
    read_lines(File,L).

When I try to call: ?- readFile.当我尝试调用时: ?- readFile. gives me an errormessage: uncaught exception: error(syntax_error('text.txt:2 (char:1). or operator expected after expression'),read/2) .给我一个错误消息: uncaught exception: error(syntax_error('text.txt:2 (char:1). or operator expected after expression'),read/2)

Thanks in advance!提前致谢!

Edit: As provided by David, GNU Prolog's Character input/output library and get_char worked for me!编辑:正如大卫所提供的,GNU Prolog 的字符输入/输出库和 get_char 为我工作!

Working code:工作代码:

readFile:-
    open('text.txt', read, File),
    read_lines(File, Lines),
    close(File),
    write(Lines), nl.

read_lines(File,[]):- 
    at_end_of_stream(File).

read_lines(File,[X|L]):-
    \+ at_end_of_stream(File),
    get_char(File,X),
    read_lines(File,L).

You can improve on your edit answer a bit.您可以稍微改进您的编辑答案。 First note that the read_line/2 predicate name is misleading.首先请注意read_line/2谓词名称具有误导性。 You're reading a text file to a list of characters, not to a list of individual lines.您正在将文本文件读取为字符列表,而不是单个行列表。 You're also calling the at_end_of_stream/1 predicate twice and creating a spurious choice-point at each call to your read_lines /2 predicate.您还两次调用at_end_of_stream/1谓词,并在每次调用read_lines /2谓词时创建一个虚假的选择点。 Moreover, the at_end_of_stream/1 predicate, albeit a standard predicate, doesn't have a reliable implementation in all Prolog systems.此外, at_end_of_stream/1谓词虽然是一个标准谓词,但在所有 Prolog 系统中都没有可靠的实现。 A possible rewrite of your code is:您的代码的可能重写是:

read_file(File, Chars) :-
    open(File, read, Stream),
    get_char(Stream, Char),
    read_file(Stream, Char, Chars),
    close(Stream).

read_file(Stream, Char, Chars) :-
    (   Char == end_of_file ->
        Chars = []
    ;   Chars = [Char| Rest],
        get_char(Stream, Next),
        read_file(Stream, Next, Rest)
    ).

Now that your intent is clearer, the same functionality can be accomplished using the Logtalk reader library with a single call:现在您的意图更清楚了,可以使用 Logtalk reader库通过一次调用来完成相同的功能:

reader::file_to_chars(File, Chars)

But, if learning is is your primary goal, writing your own solutions instead of relying on existing libraries is a good choice.但是,如果学习是您的主要目标,那么编写自己的解决方案而不是依赖现有的库是一个不错的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM