7
votes

I saw the below threads and they are very useful and related to my problem

Writing in file | Swi-Prolog | Windows\

Prolog insert, modify and delete facts on a separated database text file

I tried to use tell, told see , seen to read write from text files but I have the same result nothing is written on the file when (I open it and see that) , and on the read either the system read end_of_file or have an error displayed in a message or on the console Below is some samples of my work :

start1:-
    open('output.txt',write,OS),
    X = 'Hi all',
    write(OS,X),
    close(OS),
    open('output.txt',read,OS2),
    read(OS2,Input).


start1:-
    absolute_file_name('X.data',Abs),
    open(Abs,write,Out),
    tell(Abs),
    write('HiAll'),
    told,
    close(Out),
    open(Abs,read,In),
    see('X.data'),
    read(X),
    seen,
    write(X).
1
read(OS2,Input). expects to read terms the below code works normal readfacts:- open('output.txt',read,In), repeat, read_line_to_codes(In,X), close(In), writef(X).Omar Isaid
I think open/close are not compatible with tell/see/toldCapelliC
@CapelliC is right. You should go back and look at the examples your links point to. They don't mix and match the ISO and Edinburgh style I/O predicates (cf. swi-prolog.org/pldoc/…). You should choose one or the other and see how that works out.lurker
Dear I saved facts line by line on the file , and get it back as array of codes , when try to assert the facts in the database this operation does not success , I tried to convert to string (using string_codes) but I have the error "Type error callable expected" and try to use term_string but the error of undeclared procedure appear.Omar Isaid
I tried to read about DCG Grammar rules but it looks have to do with listsOmar Isaid

1 Answers

4
votes

Thank you very much @CapelliC the below code , which I wrote it , works fine the read built-in predicate used to read terms and when reach to end of file it shows an error , instead I used read_line_to_codes


readfacts:-
    open('output.txt',read,In),
    repeat,
    read_line_to_codes(In,X),writef(" "),
    writef(X),nl,
    X=end_of_file,!,
    nl,
    close(In).


writefacts:-
    open('output.txt',write,Out),
    write(Out,'Age(Peter,30)'),
    write(Out,'Skin(Smith,Black).'),
    close(Out).