3

For reading a file i'm using below code, but this code reads only lines where there is a '.' in the end of file, is there another way to read any kind of file and store in the list?

main :-
   open('myFile.txt', read, Str),
   read_file(Str,Lines),
   close(Str),
   write(Lines), nl.

read_file(Stream,[]) :-
    at_end_of_stream(Stream).
read_file(Stream,[X|L]) :-
    \+ at_end_of_stream(Stream),
    read(Stream,X),
    read_file(Stream,L).

I took this code from this link Read a file line by line in Prolog

Community
  • 1
  • 1
seleucia
  • 1,046
  • 4
  • 17
  • 30
  • 1
    See this answer for a summary of options for reading a file (line by line or otherwise) : http://stackoverflow.com/questions/26005369/read-from-a-file-or-stream –  Dec 20 '14 at 09:14

1 Answers1

3

Please note that the code you wrote is inherently incorrect for the following reasons.

1mo, you are reading Prolog data, and not lines at all. Take a. b. c. which would make a list [a,b,c].

2do, testing for at_end_of_stream/1 is extremely tricky, and does not work as expected when reading Prolog data. Take the 3-line file:

first_fact.
second_fact.
% no comment - oh that's a comment nevertheless

This will give you the list [first_fact, second_fact, end_of_file]. For read/1 gives you a term end_of_file after having read the last line.

3tio, upon backtracking, main will always produce an error!

?- main.
[first_fact,second_fact,end_of_file]
true ;
ERROR: at_end_of_stream/1: stream `<stream>(0x1c38330)' does not exist

If you want to read just bytes use library(pio). And to use that, first learn the formalism. The following will succeed for each occurrence of magic in the file:

?- phrase_from_file((..., "magic", ...), filename).

using the following definition of ... //0:

... --> [] | [_], ... .

See this for more.

Community
  • 1
  • 1
false
  • 10,264
  • 13
  • 101
  • 209
  • I'm very beginner at prolog, I just simple read data a from a file and write or do something else line by line. I checked the phrase_from_file and pio library usage, they seem not fit my example. basicly i want to do this list=readAllLines() foreach(line in lines) if do_something(line) ==true count++ – seleucia Dec 19 '14 at 22:23
  • @seleucia: The link at the end of my answer describes exactly this case. Doing general I/O in Prolog as a beginner is otherwise not a good idea. First learn the language without general I/O. – false Dec 19 '14 at 22:26
  • I used the link you send me, but it is generating, not my list something i don't know
    for example my data.txt have this data
    12
    34
    56
    and it is pringting something else
    59
    99
    – seleucia Dec 19 '14 at 22:33
  • 1
    @seleucia: To process such kind of data, you will need to learn [tag:dcg] first. There is no way around! – false Dec 19 '14 at 22:35
  • I don't understand your comment, i need only read file and put it in a list, this very simple operation in any other language. I'm doing this for my homework. – seleucia Dec 19 '14 at 23:24
  • I have to iterate with in this data and count how many of them fits with dcg. For example i want to check the line if it is a date or not. – seleucia Dec 19 '14 at 23:29
  • @seleucia: So you need to write a DCG first. – false Dec 20 '14 at 08:17
  • Thank you for all your answers, it help me to solve the problem. – seleucia Dec 20 '14 at 18:00