I am trying to use read_line_to_codes(Stream,Result)
and atom_codes(String,Result)
. These two predicates first, read line from file as an array of char codes, and then convert this array back to string. Then I would like to input all those strings to an array of Strings.
I tried the recursive approach but have trouble with how to actually instantiate the array to empty in the beginning, and what would be the terminating condition of process_the_stream/2
.
/*The code which doesn't work.. but the idea is obvious.*/
process_the_stream(Stream,end_of_file):-!.
process_the_stream(Stream,ResultArray):-
read_line_to_codes(Stream,CodeLine),
atom_codes(LineAsString,CodeLine),
append_to_end_of_list(LineAsString,ResultArray,TempList),
process_the_stream(Stream,TempList).
I expect a recursive approach to get array of lines as strings.
Follows a Logtalk-based portable solution that you can use as-is with most Prolog compilers, including GNU Prolog, or adapt to your own code:
Sample file for testing:
Simple test:
I sense a lot of confusion in this question.
read_line_to_codes/2
is not in its standard library.To do it in SWI-Prolog without recursion, and get strings:
If you need something else you need to explain better what it is.