Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an Equivalent scanf function in Lua?

Tags:

c

lua

scanf

As the title said:

Is there an Equivalent scanf function in Lua?

I mean, I want to have a syntax like this:

word_typed = lua_scanf()

Once I have written a string(let's say "123 is perfect number"), I have the string "123 is perfect number".

Many thanks in advance

P.S. You can give, if you want, a LUA C API solution if you want!! :P

like image 437
Azoun Avatar asked Apr 12 '26 00:04

Azoun


1 Answers

The following script (tested with Lua 5.1.4, should work in 5.2) does what you describe in your comment to Shmil-The-Cat's answer:

print("Please enter your name:")
name = io.read()
print("You entered '" .. name .. "'")

The io.read function has a default parameter of "*l" which means to read the next line from the open file, skipping the end of line. The open file, by default is stdin but you can change it using the io.open file.

For more details on the io library, see section 6.8 of the online Lua reference manual, which can be found at http://www.lua.org/manual/5.2/manual.html#6.8. You might also want to read the I/O chapter of Programming in Lua (PIL) which can be found at http://www.lua.org/pil/21.html.

like image 74
Matthew Burke Avatar answered Apr 14 '26 00:04

Matthew Burke