Reputation: 11
I want to read a file and then write another according to the results of the first, but once I read the first file, the program terminates immediately. I have searched for the cause of why it happens, but I have not found anything. I would like to know if someone can help me.
This is the code:
main :-
current_prolog_flag(argv, Argv),
[H|_T] = Argv,
readPlainText(H),
getList(L),
open('result.txt',write,X),
write(X, L), nl(X),
close(X),
halt.
main :-
halt(1).
And the script stops after execute this piece of code:
readPlainText(X) :-
open(X, read, Stream),
readWords(Stream),
close(Stream).
I am executing from console:
swipl -s leer.pl --quiet menciones.txt
EDIT: I think the problem is for at_end_of_stream
readWords(InStream) :-
\+ at_end_of_stream(InStream),
readWord(InStream, W),
write(W), nl,
addWordToDatabase(W),
readWords(InStream).
Upvotes: 1
Views: 116
Reputation: 60004
You're missing the recursion termination. Try
readWords(InStream) :-
( \+ at_end_of_stream(InStream)
-> readWord(InStream, W),
write(W), nl,
addWordToDatabase(W),
readWords(InStream)
; true
).
Or you can keep unchanged your readWords/1, and add - after it - the handler for EOF:
readWords(_InStream).
Then you could test for errors possibily occurring in readWords/1 goals (for instance, in addWordToDatabase/1), that currently would stay hidden.
Upvotes: 1