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).
In order to write to a file we have to create one (or open an existing one) and associate a stream with it. You can think of streams as connections to files. In Prolog, streams are blessed with names in a rather user-unfriendly format, such as '\$stream'(183368) .
Double-click the Prolog program icon. Use the File menu of the resulting window to create a new file and give it a name, with the extension . pl, making sure you save it at the root level of your M drive. .
Open a terminal (Ctrl+Alt+T) and navigate to the directory where you stored your program. Open SWI-Prolog by invoking swipl . In SWI-Prolog, type [program] to load the program, i.e. the file name in brackets, but without the ending. In order to query the loaded program, type goals and watch the output.
Before Prolog accepts the input from user, we have to press the 'return' key. When Prolog evaluates a read goal, the input term is unified with the variable of the argument. If the variable of the argument is unbound, it is bound to the input value.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With