tutorial on file I/O operations in LUA

Source: Internet
Author: User
Tags file system flush lua

This article mainly introduces the file I/O operations in LUA Tutorial, is the basic knowledge in Lua learning, need friends can refer to the

The I/O library in Lua is used to read and process files. There are two types of file operations, in Lua, the implied file descriptor and an explicit file descriptor.

For the following example, we will use a sample file Test.lua, as shown in the following figure.

The code is as follows:

--Sample Test.lua

--Sample2 Test.lua

A simple file-opening operation uses the following statement.

The code is as follows:

FILE = Io.open (filename [, mode])

The various file modes are listed in the following table.

Hidden file descriptor

The implicit file descriptor uses a standard input/output mode, or a single input single output file. An example of using an implicit file descriptor is shown below.

The code is as follows:

--Opens a file in read

File = Io.open ("Test.lua", "R")

--Sets the default input file as Test.lua

Io.input (file)

--Prints the ' the ' file

Print (Io.read ())

--Closes the open file

Io.close (file)

--Opens a file in Append mode

File = Io.open ("Test.lua", "a")

--Sets the default output file as Test.lua

Io.output (file)

--Appends a word test to the last line of the file

Io.write ("--end of the Test.lua file")

--Closes the open file

Io.close (file)

When you run the program, you get the first line of output from the Test.lua file. The following output is obtained in this example.

The code is as follows:

--Sample Test.lua

This is the first line of the declaration Test.lua file. "--end of the Test.lua file" will be appended to the last line of the Test.lua code

In the above example, you can see the implicit description and the use of file system IO. " How the X method works. The above example uses Io.read () with no optional parameters. Optional parameters can be any of the following.

Other common IO methods include the following:

Io.tmpfile (): Return to read and write temporary files, once the program exits, the file will be deleted.

Io.type (file): Returns files, closes the file or 0 according to the entered file.

Io.flush (): Clears the default output buffer.

Io.lines (optional file name): Provides a generic loop iterator to traverse a file and close the file that provides the file name and default file in the last case is used and is not closed at the end of the loop.

Clear file Descriptor

We often use explicit file descriptors to enable us to process multiple files at the same time. These features are quite similar to the implicit file descriptor. Here we use the file: The function name, not the Io.function_name. As well as the file version of the file descriptor example, the following is shown below.

The code is as follows:

--Opens a file in read mode

File = Io.open ("Test.lua", "R")

--Prints the ' the ' file

Print (File:read ())

--Closes the opened file

File:close ()

--Opens a file in Append mode

File = Io.open ("Test.lua", "a")

--Appends a word test to the last line of the file

File:write ("--test")

--Closes the open file

File:close ()

When running the program, the implied description of the example is similar to the output.

The code is as follows:

--Sample Test.lua

File open and parameters are read outside the description of all patterns is the same as the implied file descriptor.

Other common methods of the file include:

File:seek (optional whence, optional offset): parameter "set", "cur" or "end". Sets the location of the file where the new file pointer is updated from the beginning of the file. The offset is the 0 basis for this feature. The offset measured from the beginning of the file if the first argument is "set", from the current position in the "cur" file, or from the end of the file, if it is "ended." The default parameter values are "cur" and 0, so the current file location can be obtained by calling the function without parameters.

File:flush (): Clears the default output buffer.

Io.lines (optional file name): Provides a generic loop iterator to traverse a file and close the file that provides the file name and default file in the last case is used and is not closed at the end of the loop.

An example to use is the search method shown below. Offsets the end of the file from the 25-position cursor. Prints the remaining seek location from the read out feature of the file.

The code is as follows:

--Opens a file in read

File = Io.open ("Test.lua", "R")

File:seek ("End",-25)

Print (File:read ("*a"))

--Closes the opened file

File:close ()

Will get some output similar to the following.

The code is as follows:

Sample2 Test.lua

--test

You can use a variety of different patterns and parameters to understand the Lua file manipulation capabilities.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.