How to generate a file by reading its name in SWI-Prolog?

147 views Asked by At

I'm writting a swi-prolog program which reads a file name, opens it, and write some stuff.

main :- read(FileName),
        tell(FileName),
        write("Some stuff"),
        told.

It works, but I must put the name of the file as '«name».«type»', like this:

?- main.
|: 'Hello.txt'.
true.

I need the program to receive the name this way, without the single-cuotes:

?- main.
|: Hello.
true.

And the file's type must be put by me:

$ ls
Hello.«whatever»

Any ideas? Thanks

1

There are 1 answers

0
Paulo Moura On

Given that you're using SWI-Prolog, you can use the readutil:read_line_to_codes/2 library predicate:

?- use_module(library(readutil)).
true.

?- current_input(Stream), read_line_to_codes(Stream, FileNameCodes), atom_codes(FileName, FileNameCodes).
|: Foo
Stream = <stream>(0x10516c440),
FileNameCodes = [70, 111, 111],
FileName = 'Foo'.

If you also need to add a file name extension to the file name you read, use the standard atom_concat/3 predicate.