【问题标题】:How read a file and write another file in prolog如何在 prolog 中读取一个文件并写入另一个文件
【发布时间】:2013-05-31 11:14:36
【问题描述】:

我想读取一个文件,修改行并将结果写入另一个文件。

读取文件:- 打开('inputfile.txt',读取,Str), read_file(Str,Lines), 关闭(Str)。 读取文件(流):- at_end_of_stream(流)。 读取文件(流):- \+ at_end_of_stream(流), 读取(流), 修改(流,流 2), write_file(Stream2), 读取文件(流)。 write_file('outputfile.txt', 短语) :- open('outputfile.txt', write, Stream), writeln(流,短语), 关闭(流)。

【问题讨论】:

    标签: prolog


    【解决方案1】:

    我会写类似的东西

    tranform_file :-
        open('inputfile.txt', read, I),
        open('outputfile.txt', write, O),
        transform_lines(I, O),
        close(O),
        close(I).
    
    transform_lines(I, O) :-
       read_line_to_codes(I, L),
       (  L == end_of_file
       -> true
       ;  transform_line(L, T),
          format(O, '~s~n', [T]),
          transform_lines(I, O)
       ).
    

    (注:未经测试)

    【讨论】:

      猜你喜欢
      • 2021-01-23
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 2018-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多