I tried to write a simple program which reads a file, and prints it to
the standard output.
fussal(X):-
% see("parse.pl"),
open("parse.pl",read,[alias(input)]),
repeat,
read(input,T),print(T),T=end_of_file,!,X=false.
Result of running:
| fussal(X).
ERROR: open/3: Type error: `atom' expected, found `[112, 97, 114, 115,
101, 46, 112, 108]'
Exception: (8) open([112, 97, 114, 115, 101, 46, 112, 108], read,
[alias(input)]) ?
fussal(X):-
X = "parse.pl",
see("parse.pl"),
seeing(X),
read(T),display(T).
Running it:
| fussal(X).
ERROR: see/1: Type error: `atom' expected, found `[112, 97, 114, 115,
101, 46, 112, 108]'
Exception: (8) see([112, 97, 114, 115, 101, 46, 112, 108]) ? creep
6 ?-
> fussal(X):-
> % see("parse.pl"),
> open("parse.pl",read,[alias(input)]),
> repeat,
> read(input,T),print(T),T=end_of_file,!,X=false.
>
> Result of running:
>
>
> | fussal(X).
> ERROR: open/3: Type error: `atom' expected, found `[112, 97, 114, 115,
> 101, 46, 112, 108]'
> Exception: (8) open([112, 97, 114, 115, 101, 46, 112, 108], read,
> [alias(input)]) ?
The message is saying that it expects an atom where it gets something else.
And that it is in the open/3 predicate.
That should ring a bell, no ?
Replace the " by '
Cheers
Bart Demoen
Try using single quotes rather than the double quotes. This
allows an atom to be formed using varied characters besides
the default syntax for an atom.
Double quotes turn the contents into a list of characters,
as you've seen above.
regards, chip
Thanks for the comments. I wasn't aware of the differences between '
and "
You just highlighted my ignorance of the Prolog type system. (I know,
Prolog is called untyped. )
Another problem: it doesn't find the file (although I checked, it's
there). I even tried using double blackslahses, but I got:
ERROR: seeing/1: Domain error: `stream' expected, found `C:\Documents
and Settings\Levente\My Documents\parse.pl'
I even tried to change "\" to "/".
The current version of my code looks like:
fussal(X):-
X = 'C:\Documents and Settings\Levente\My Documents\parse.pl',
see(X),
seeing(X),
read(T),display(T).
fussal(X):-
X = 'c:/parse.pl',
see(X),
% This will give an error, because it expects a stream;
% seeing(X),
repeat,
get_char(T),print(T),T=end_of_file,!,seen.
The conclusions:
1,
If you omit the "seen" from the end of the end of the last line, the
next time you run the program you'll experience that the program only
reads "end_of_file" signs. This is because the "cursor position" of
file reading is assigned to the opened stream. (And a stream acts like
a global variable.)
2,
read/1 is for parsing prolog files. (It can handle the DCG syntax as
well.)
But you started with ISO style. The typical loop using read/2 is:
process(File) :-
open(File, read, In),
read(In, Term1),
process_stream(Term1, In),
close(In).
process_stream(end_of_file, _) :- !.
process_stream(Term, In) :-
<do something with Term>
read(In, Term2),
process_stream(Term2, In).
ISO says nothing on how files are specified, though most systems accept
a (quoted) atom holding the filename. Generally they accept OS native
syntax, which means in Windows you need 'C:\\Program Files\\...'. Often
they also accept the POSIX / syntax as a portable syntax.
Cheers --- Jan