Updated April 10, 2023
Introduction to Lua write to file
The contents can be written into the file or read from the file in Lua using I/O library and to perform any operation on a file, a file must be opened and a file can be opened in three standard modes such as write mode, read mode and append mode and the mode in which we are going to open the file must be specified in file open operation and opening the file in write mode enables us to write the data to the file and once the operation on the file is complete, the file must be closed using file close operation.
Syntax to write data to the file in Lua:
file_descriptor = io.open(file_name, mode)
file_descriptor:write(“Data to be written to the file”)
Where,
- io.open() operation is used to open the file.
- file_name specifies the name of the file to be opened.
- mode is the mode in which the file must be opened. The file can be opened in read mode(r), write mode(w) or append mode(a).
- file_descriptor:write() operation is used to write the contents to the file.
- Data to be written to the file is the data that is to be written to the file.
Working of write to file Operation in Lua
- Several operations can be performed on a file such as reading the data from the file, writing the data to a file and appending the data to the existing data in a file.
- The operations such as reading the data from the file, writing the data to a file and appending the data to the existing data in a file can be performed in Lua using I/O library.
- The first step to perform any operation on the file is to open the file.
- A file can be opened using io.open() function.
- The mode in which the file is opened specifies what operation can be performed on the file that is opened.
- There are three modes in which a file can be opened, they are read mode(r), write mode(w) and append mode(a).
- Opening the file in write mode enables us to write the data to the file.
- Once the operation on the file is complete, the file must be closed using file close operation.
Examples of Lua write to file
Given below are the examples of Lua write to file:
Example #1
Lua program to open a file in write mode and write the contents to the file using write() function and then read the contents from the file using read() operations and then display the contents of the file as the output on the screen.
Code:
--creating a temporary file using io.tempfile function
tempfile = io.tempfile
--opening the file in write mode to write the contents to the file using write() operation and then closing the file
filewrite = io.open("tempfile", "w")
filewrite:write("Welcome to EDUCBA")
filewrite:close()
--opening the file in read mode to read the contents from the file using read() operation to display the contents of the file and then closing the file
fileread = io.open("tempfile", "r")
print("The contents in the file are:\n")
print(fileread:read())
fileread:close()
Output:
In the above program, we are creating a temporary file using io.tempfile function. Then we are opening the file in write mode to write the contents to the file using write() operation and then closing the file. Then we are opening the file again in read mode to read the contents from the file using read() operation to display the contents of the file and then closing the file.
Example #2
Lua program to open a file in write mode and write the contents to the file using write() function and then read the contents from the file using read() operations and then display the contents of the file as the output on the screen.
Code:
--creating a temporary file using io.tempfile function
tempfile = io.tempfile
--opening the file in write mode to write the contents to the file using write() operation and then closing the file
filewrite = io.open("tempfile", "w")
filewrite:write("Learning is fun")
filewrite:close()
--opening the file in read mode to read the contents from the file using read() operation to display the contents of the file and then closing the file
fileread = io.open("tempfile", "r")
print("The contents in the file are:\n")
print(fileread:read())
fileread:close()
Output:
In the above program, we are creating a temporary file using io.tempfile function. Then we are opening the file in write mode to write the contents to the file using write() operation and then closing the file. Then we are opening the file again in read mode to read the contents from the file using read() operation to display the contents of the file and then closing the file.
Example #3
Lua program to open a file in write mode and write the contents to the file using write() function and then read the contents from the file using read() operations and then display the contents of the file as the output on the screen.
Code:
--creating a temporary file using io.tempfile function
tempfile = io.tempfile
--opening the file in write mode to write the contents to the file using write() operation and then closing the file
filewrite = io.open("tempfile", "w")
filewrite:write("We love India")
filewrite:close()
--opening the file in read mode to read the contents from the file using read() operation to display the contents of the file and then closing the file
fileread = io.open("tempfile", "r")
print("The contents in the file are:\n")
print(fileread:read())
fileread:close()
Output:
In the above program, we are creating a temporary file using io.tempfile function. Then we are opening the file in write mode to write the contents to the file using write() operation and then closing the file. Then we are opening the file again in read mode to read the contents from the file using read() operation to display the contents of the file and then closing the file.
Recommended Articles
We hope that this EDUCBA information on “Lua write to file” was beneficial to you. You can view EDUCBA’s recommended articles for more information.