Lua standard library-input and output facilities)

Source: Internet
Author: User

The I/O Library provides two different methods for file processing.

1. IO table call method: IO table is used. Io. Open will return the description of the specified file, and all operations will be described around this file.

IO tables also provide three predefined file descriptions: Io. stdin, Io. stdout, Io. stderr

2. The file handle is called directly by using the file: XXX () function, where file is the file handle returned by Io. open ().

If most I/O functions fail to be called, nil plus error messages are returned. If some functions are successful, Nil is returned.

1. Io. Close ()

Function: equivalent to file: Close (). Close the default output file.

2. Io. Flush ()

Function: equivalent to file: flush (), which outputs All cached content to the default output file.

3. Io. Lines ([filename])

Function: Open the specified file filename in Read mode and return an iteration function. Each call will obtain a row of content in the file. When it reaches the end of the file, it will return nil and automatically close the file.

Io. Lines () <=> Io. Input (): lines (); reads the content of the default input device, but does not close the file at the end.

For example, for line in Io. Lines ("Main. Lua") Do

Print (line)

End

4. Io. Open (filename [, mode])

Function: open a file in the specified mode. If the file is successfully opened, the file handle is returned. If the file fails, the NIL + error message is returned.

Mode:

"R": Read mode (default );

"W": Write mode;

"A": Add mode;

"R +": update mode. All previous data will be saved.

"W +": update mode. All previous data will be cleared.

"A +": Add update mode. All previous data will be saved and can be added only at the end of the file.

"B": Some Systems Support binary

5. Io. Output ()

Function: It is equivalent to Io. input, but the operation is performed on the default output file.

6. Io. popen ([prog [, mode])

Function: start the program prog on an additional process and return the file handle for prog (not all system platforms are supported)

7. Io. Read (...)

Function: equivalent to Io. Input (): Read

8. Io. tmpfile ()

Function: returns a temporary file handle, which is opened in update mode and automatically deleted when the program ends.

9. Io. Type (OBJ)

Function: checks whether obj is an available file handle.

Return Value:

"File": An opened file handle.

"Closed file": A closed file handle.

Nil: indicates that obj is not a file handle.

10. Io. Write (...)

Function: equivalent to Io. Output (): Write

11. File: Close ()

Function: Disable a file.

Note: When the file handle is garbage collected, the file is automatically closed. The handle changes to an unpredictable value.

12. File: flush ()

Function: Write all data in the buffer to a file.

13. File: lines ()

Function: returns an iteration function. Each call will obtain a line of content in the file. when the end of the file is reached, nil will be returned, but the file is not closed.

For example, for line in file: lines () do body end

14. File: Read (...)

Function: reads a file in the specified format. A string or number is returned for each format function. If the file cannot be correctly read, Nil is returned, if no format is specified, the system reads data by row by default.

Format:

"* N": reads a number.

"* A": reads the entire file from the current position. If it is the end of the file, an empty string is returned.

"* L": [Default] reads the content of the next row. If it is the end of the file, Nil is returned.

Number: reads the characters of the specified number of bytes. If it is the end of the file, Nil is returned. If number is 0, an empty string is returned. If it is the end of the file, Nil is returned;

15. File: Seek ([whence] [, offset])

Function: Sets and obtains the current file location. If the file location is successful, the final file location (by byte) is returned. If the file location fails, the NIL plus error message is returned.

Parameters

Whence:

"Set": Starting from the file header

"Cur": Starting from the current position [Default]

"End": starting from the end of the file

Offset: The default value is 0.

If file: Seek () is not included, the current position is returned. If file: Seek ("set") is located, the file header is located. If file: Seek ("end ") locate the end of the file and return the file size.

16. File: setvbuf (mode, [, size])

Function: sets the buffer mode for the output file.

Parameters

Mode:

"No": no buffer, that is, direct output

"Full": Full buffer, that is, the output operation is performed only when the buffer is full (you can also use flush to output immediately)

"Line": outputs in behavior units (mostly used for terminal devices)

In the last two modes, the size can specify the buffer size (byte). Ignoring the size will automatically adjust to the optimal size.

17. File: Write (...)

Function: output the file content in the specified parameter format. The parameter must be a character or number. to output other values, use tostring or string. Format for conversion.

1

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.