How read a file and write another file in prolog

前端 未结 1 1301
不思量自难忘°
不思量自难忘° 2021-01-28 06:17

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


        
相关标签:
1条回答
  • 2021-01-28 06:41

    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)

    0 讨论(0)
提交回复
热议问题