| newser.bbs@bbs.ee.ncu.edu.tw 2006-05-17, 4:03 am |
| Andreas Kochenburger wrote:
> Don't reinvent the wheel:
> www.hta-bi.bfh.ch/~hew/prolog/chapter6/turbo.pl
I have tried the code in that site , and some codes insides seems
functioning wrongly.
I use a file named 1.txt in the root directory of driver C to test the
eof/1
of that webpage , and the contents of the file 1.txt are :
First line
Last line
Please look for the results:
---------------------------------------------------------------------------------------
Welcome to SWI-Prolog (Multi-threaded, Version 5.6.12)
Copyright (c) 1990-2006 University of Amsterdam.
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
Please visit http://www.swi-prolog.org for details.
For help, use ?- help(Topic). or ?- apropos(Word).
1 ?- assertz((eof(S) :-
| current_stream(F,_,S), size_file(F,N), character_count(S,C),
C >= N)).
S = _G765
F = _G767
N = _G772
C = _G775
Yes
2 ?-
assertz((get_line(L):-get_char(C),(C=end_of_file->copy_term('',L);C='\n'-> copy_term(C,L);get_line(L1),atom_concat(
C,L1,L)))).
L = _G932
C = _G934
L1 = _G954
Yes
3 ?- see('c:\\1.txt').
Yes
4 ?- seeing(S).
S = '$stream'(1070510764)
Yes
5 ?- get_line(L).
L = 'First line\n'
Yes
6 ?- seeing(S).
S = '$stream'(1070510764)
Yes
7 ?- get_line(L).
L = 'Last line\n'
Yes
8 ?- get_line(L).
L = ''
Yes
9 ?- seeing(S).
S = '$stream'(1070510764)
Yes
10 ?- seeing(S),eof(S).
No
11 ?-
-------------------------------------------------------------------------------------------
See ? The eof/s doesn't know that the end of file was reached .
The following is the result by my codes :
++++++++++++++++++++++++++++++++++++++++
++++++
GNU Prolog 1.2.16
By Daniel Diaz
Copyright (C) 1999-2002 Daniel Diaz
| ?-
assertz((get_line(L):-get_char(C),(C=end_of_file->copy_term('',L);C='\n'-> copy_term(C,L);get_line(L1),atom_concat(
C,L1,L)))).
yes
| ?- assertz((eof:- get_char(C),(C=end_of_file,!;unget_char(
C),fail))).
yes
| ?- see('c:\\1.txt').
yes
| ?- eof.
no
| ?- get_line(L).
L = 'First line\n'
yes
| ?- eof.
no
| ?- get_line(L).
L = 'Last line\n'
yes
| ?- eof.
yes
| ?-
++++++++++++++++++++++++++++++++++++++++
+
See , the eof/0 by me knows clearly whether the end of
file is reached.
I don't want to reinvent the wheel either , but if what I get
is a wheel that doesn't whirl , what can I do ? Just
invent a real wheel .
By the way , some of you might ask why don't I test in just
one prolog implement ? Is that the real reason for these
different results?
That's because I can't find unget_char in SWI prolog .
And that's why I asked for the way of unget in SWI's.
Because it is very important in building a real function
for eof.
|