Skip to content

Instantly share code, notes, and snippets.

@rozer007
Created December 4, 2021 07:34
Show Gist options
  • Save rozer007/489372efaaab511023308b5285e0ff21 to your computer and use it in GitHub Desktop.
Save rozer007/489372efaaab511023308b5285e0ff21 to your computer and use it in GitHub Desktop.
File IO in Lua
Q1. What does a+ mode means in lua?
ans: a+ mode is the mode where we can append,read and create a file.
Q2. How can we create a file in lua?
ans: We can open the file in w+ , if the file doesn't exist it will create a new file.
Q3. what does seek() do in lua?
ans: seek() function is use to place the filepointer to a specific position.
Q4. What does close() function do?
ans: This will close an opened file. If you close a file ,it can't be read or written anymore.
Q1. What will be the output of this statement?
file=io.open("test.txt","w+")
file:write("Hello Pepcoder\n")
file:seek("end",-9)
print(file:read())
a) Error
b) Pepcoder
c) Hello
d) ello pepcoder
ans: b)
Q2. What will be the output of this statement?
file=io.open("test.txt","a+")
file:write("Hello Pepcoder\n")
file:write("Hello Pepcoder1\n")
file:seek("set",5)
print(file:read())
a) Hello Pepcoder
b) Pepcoder
c) Hello Pepcoder
Hello Pepcoder1
d) Pepcoder1
ans: b)
Q3. What will be the output , consider the same file as in previous question?
file=io.open("test.txt","w+")
file:write("Bye pepcoder")
file:seek("set",0)
print(file:read("*a"))
a) Hello Pepcoder
Hello Pepcoder1
Bye pepcoder
b) Hello Pepcoder
Hello Pepcoder1
c) Bye pepcoder
d) none of the above
ans: c)
Q4. What is the correct way to write to a file?
a) io.write()
b) file_name.write()
c) file_name:write()
d) none of the above
ans:c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment