How to read a file in Prism?

9

I was trying to read a file in Prism but I do not find the correct function:

local arquivo = 'arquivo.txt'
imprima(arquivo)

On moon, it would be something like local file = 'file.txt'; print(file); .

    
asked by anonymous 11.07.2017 / 20:42

1 answer

9

On the Moon

The code below reads the entire contents of a file:

arquivo = 'arquivo.txt'
handle = io.open(arquivo, "r")
conteudo = handle:read("*a")
handle:close()
print(conteudo)

If you want to handle errors (recommended!), use something like

handle,message = io.open(nome, "r")
if handle==nil then error(message) end

In Prism

In Prism the code is similar, just translate keywords, method names and options:

arquivo = 'arquivo.txt'
handle = es.abra(arquivo, "leitura")
conteudo = handle:leia("*t")
handle:feche()
imprima(conteudo)
    
12.07.2017 / 13:39