Updated April 11, 2023
Definition of Lua File
File is used to store data, read data, and so on like other programming languages. In Lua, we have a different library that can handle files in Lua. Lua support the various built-in library by the help of which we can handle our file in Lua programming, we can open, close or read from a file by using this library. File I/O library is the most common one to use in Lua. By the use of this, we can export and import the data from a large file into our programming, I/O library provides us several features by which we can handle our files in Lua efficiently. In the coming section, we will discuss more file in Lua in detail and how to use and implement this while programming.
Syntax:
As we discussed file I/O can be used to deal with files in Lua it is a built-in library, let’s take a look at its syntax how to use this while programming in detail, see below;
io.open([Your_file_name] [, [your_mode]])
As you can see in the above lines of syntax, this is the basic syntax to open a file using IO in Lua. Inside this, we are mentioning the filename followed by the mode in which we want our file to be handled. let’s take a practice syntax for beginners to understand it in a better way see below;
e.g. :
io.open( "demo.lua" , "r"] )
How Lua file works?
As we already discussed file is used to store a large amount of data, by the use of a file we can export and import a large amount of data from the file. It is very easy to read large data from the file. In Lua file Io works in the same way like C programming language, it divides it into two basic modes which is as follows;
1) simple mode: Simple mode in Lua deals with the current input and output files. It also provides us some operations which is related to these files only.
2) complete mode: this is also known as Full mode, it uses an external handle to achieve this.
Now we will take look at all the operations that can we perform by using this IO library in Lua see below;
1) Open a file: To open a file we can use io. open() function which takes two parameters as the input param, one is the file name another mode is the mode for the file. In the coming section of the tutorial, we will discuss more the modes of file in detail.
2) to close a file: We can also close an existing file if we do not want to perform any further operation on it. For this IO library provide us io.close() function.
e.g.:
io.close(file_name)
As you can see in the above lines of syntax we can pass the file name inside the close() function so it will close the respective file for us.
3) write to a file: If you want to write to an existing file then we have write() function available for this. Below see the syntax for better understanding;
e.g. :
io.write("Your string")
As you can see in the above lines of code we are passing our string inside the write() function, this will add this string at the end of the file.
4) To read file: Io library also provides our function by the help of which we can read the file content. It provides us read() function for this, we can call this function o IO object. let’s take look at its syntax how it looks;
e.g. :
io.read();
As you can see from the above lines of code we are trying to read the file content, for this we are calling read() function. It will return us the first line from the mentioned file.
Now we will discuss different modes of file in detail see below;
1) “r”: This is the default mode of the file, it enables the ready only mode.
2) “a”: This stands for append mode, to will open the existing file or it will create a new file for us.
3) “w”: This stands for write mode in IO library, it will either create a new file or override the existing one.
4) “r+”: This represents read and write both modes for a file.
5) “a+”: This represents read and append mode on the file. it will either create a new file or it will open the existing one for us.
6) “*a”: This mode will help us to get the content of the file but from the current position of the file.
7) “*l”: This also helps us to read the file from the current position, after reading it will move the position to the next line of the file.
We have some methods available by the Io library which are as follows;
1) io.type() : It return the type of file. Inside this, we can pass our file, and it will tell us whether it is an open, close or nil file.
e.g. :
io.type(your_file)
2) io.flush(): As it, the name suggests it will clear out the default buffer.
e.g. :
io.flush()
3) io.tmpfile(): As its name suggests it will return us the temporary file. On this file, we can perform two operations that is read and write, once we close or quit the program this file will be removed.
4) io.lines(): This provides us the iterator which will iterator through the complete file.
Example
In this example we are trying to create a file with mode “w”, we are reading and writing to the file and printing the file content using read() function from IO library of Lua. This is a sample example for beginners to understand it better and implement this while programming in Lua.
Code:
print("Demo to show file IO functionality in lua !!")
myfile = io.open("demo.lua", "w")
io.input(myfile)
myfile = io.open("demo.lua", "r")
print("File created success fully !!")
print("print the content of file ::")
print(io.read())
print("writing to the file ::")
io.write("This is sample program to show file in lua !!")
print(io.read())
io.close(myfile)
Output:
Conclusion
To handle file in any programming language we would require a library which will help us to read, write, close, etc to perform operations on the file. In Lua we have IO library for this, which provide us some methods and mode to handle the file, it is an inbuilt library in Lua which works in the same way like C language.
Recommended Articles
We hope that this EDUCBA information on “Lua File” was beneficial to you. You can view EDUCBA’s recommended articles for more information.