theltrtduck
theltrtduck

Reputation: 49

Trying to get a program in Lua to read info from individual lines in a file

OK, so I have a file with three lines. I want a program to read from these lines and print the info.

Here's what I have so far:

print("Which user do you want to view?")
account = read()
file = io.open(account, "r")
name = io.read()
owe = io.read()
balance = io.read()
print("Their name is " .. name .. ".")
print("They owe us " .. owe .. ".")
print("They have " .. balance .. " in their account.")

When i run the program, it doesn't even come up with an error, just nothing happens. I have no idea what's going wrong...

Upvotes: 2

Views: 50

Answers (1)

Yu Hao
Yu Hao

Reputation: 122383

io.read() reads from the current input file. By default, it's the standard input. You need to change it using io.input().

--...
f = io.open(account, "r")
io.input(f)  # here
name = io.read()
owe = io.read()
balance = io.read()
--...

Another option is specify where to read from explicitely:

--...
f = io.open(account, "r")
name = f:read()
owe = f:read()
balance = f:read()
--...

Upvotes: 3

Related Questions