本文介绍了如何在prolog中读取文件并写入另一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想读取一个文件,修改行并将结果写入另一个文件.
读取文件:-open('inputfile.txt', read, Str),read_file(Str,Lines),关闭(Str).读取文件(流):-at_end_of_stream(流).读取文件(流):-\+ at_end_of_stream(流),读取(流),修改(流,流2),写文件(流2),读取文件(流).write_file('outputfile.txt', 短语) :-open('outputfile.txt', write, Stream),writeln(流,短语),关闭(流). 解决方案
我会写类似的东西
transform_file :-open('inputfile.txt', read, I),open('outputfile.txt', write, O),变换线(I,O),关闭(O),关闭(一).transform_lines(I, O) :-read_line_to_codes(I, L),( L == end_of_file->真的;变换线(L,T),格式(O, '~s~n', [T]),变换线(I,O)).
(注:未经测试)
I would like to read a file, modify lines and write the results to another file.
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).
解决方案
I would write something like
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)
).
(note: untested)
这篇关于如何在prolog中读取文件并写入另一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!