users-prolog
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Reading from files?


From: carlos calderon
Subject: Reading from files?
Date: Thu, 14 Jun 2001 19:10:03 +0100

HI all,
 
As I said in some of my previous e-mails I am new to Prolog and GNU-Prolog and therefore I apologise if some of the questions are too simple or just completely nonsense.
 
I am having problems with what I think is a simple thing: reading from files.
 
go(X,Y) :-
            solve(X,Y),
            write2file(X,Y).
 
 
solve(X,Y) :-
            /*Vars, domains and constraints are defined here*/
            /*it works fine no problem*/
 
write2file :-
    open('/home/carlos/gnu_prolog/example/files/test.txt', append, Out,[]),
    write_term(Out,X,[]),
    write_term(Out,Y,[]),
    close(Out,[force(true)])./*to be sure that the stream is closed*/
 
>> with this I got the following:
111213182122272831363738464748565758656667687176777881828788
>>all the possible solutions i.e X=1 Y=1 is the first one
>>Great!! ;-)))
 
Then I want to read this file 'test.txt'
Ideally, every time a solution is written to test.txt I would like to have access to it and read it.
 
So I did the following:
 
 
go(X,Y) :-
            solve(X,Y),
            write2file(X,Y),
            read_file_to_list(Result).
write2file :-
    open('/home/carlos/gnu_prolog/example/files/test.txt', append, Out,[]),
    write_term(Out,X,[]),
    write_term(Out,Y,[]),
    close(Out,[force(true)])./*to be sure that the stream is closed*/
 
read_file_to_list(Array) :-
      open('/home/carlos/gnu_prolog/example/files/test.txt', read, In,[type(text)]),
       set_input(In),
        set_stream_buffering(In,none),
        get_char(In,Char),
        (Char = end_of_file
                -> Array=[]
        ;
                Array = [Char | T],
                read_file_to_list(T)
       ),
    close(In).
 
>>I get the following result:
in my text file:
11
>>just the first solution
>>and the following error:
uncaught exception: error (resource_error(too_many_open_streams), open/4)
pag 40 in the manual.
any ideas about what it's happening?
 
I guess, my real question is: is there any "standard" method to read files?
 
again, sorry for the simplicity but if I don't ask stupid questions I remain stupid.
 
I have also tried to use I/O to constant terms.
 
go(X,Y) :-
            solve(X,Y),
            write2chars(X,Y).
 
write2chars(X,Y) :-
            write_to_chars([X|L1],X),
            write_to_chars([X|L1],Y),
            read_from_chars([X|L1],_A),
            read_from_chars([X|L1],_A),
            set_output(user_output),
            put_char(_A),
            put_char(_B).
 
>>This time I get an empty test.txt and a no to my query go(X,Y).
 
My final objective is to have a precise control over the streams (have a clear understanding first) for communication purposes (sockets).
 
As far as my understanding goes there are no tutorials or examples on web but if anyone knows of any please, I would be very grateful if he/she lets me know.
 
Thanks for the help
           
 
 
 
                               

reply via email to

[Prev in Thread] Current Thread [Next in Thread]