How to read a file in Prism?

Asked

Viewed 219 times

9

I was trying to read a file in Prisma but I can’t find the correct function:

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

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

1 answer

9


In Lua

The code below reads the full 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 Prisma

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)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.