如何读取文件并在prolog中写入另一个文件

时间:2013-05-31 11:14:36

标签: prolog

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


readtofile :-
    open('inputfile.txt', read, Str),
    read_file(Str,Lines),
    close(Str).


read_file(Stream) :-
    at_end_of_stream(Stream).

read_file(Stream) :-
    \+ at_end_of_stream(Stream),
    read(Stream),
    modify(Stream,Stream2),
    write_file(Stream2),    
    read_file(Stream).      


write_file('outputfile.txt', Phrase) :-
    open('outputfile.txt', write, Stream),
    writeln(Stream, Phrase),
    close(Stream).  

1 个答案:

答案 0 :(得分: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)
   ).

(注意:未经测试)