In PIXERA Modules, you can use the IO Library to control the system's files, including loading, reading, and writing from and to files. In this article, we will go over the calls and their uses.
Note that all mentioned calls can be used in two ways.
The first is by using “io.close()” for example, this will close the default file, which is the last file opened.
The second is by using “FileName:close()”, this will close the file with the given variable name, this is the recommended route to make sure you are being explicit in what is being modified.
Calls
- close()
- Closes the file, executing the pending writes of the file/saving it in the drive. If not called or a flush called before the script ends, the file will not execute/save and all changes will be lost.
- flush()
- Executes all pending writes to a file.
- input()
- Opens the file in text mode and sets its handle as the default input file if using a filename. If using a handle will set the default input file with the given handle. If called without any arguments, it returns the default input file handle.
- lines()
- Opens the file in read mode and returns it as an iterator function that works with for pair loops to go line by line through the file. Once finished, the given file will close automatically. If no file is given, will use the default input file and will not close it once finished.
- open(mode)
- Opens a file in a mode from the given mode name. If a success, it returns a new file handle. The possible mode string values can include:
- “r”: Read Mode (Default)
- “w”: Write Mode
- “a”: Append Mode
- “r+”: Update Mode, all previous data is preserved
- “w+”: Update Mode, all previous data is erased
- “a+”: Append Update Mode, previous data is preserved, writing is only allowed at the end of file.
- The mode string can also have a “b” at the end, which is needed in some systems to open the file in binary mode.
- Opens a file in a mode from the given mode name. If a success, it returns a new file handle. The possible mode string values can include:
- output()
- Opens the file in text mode and sets its handle as the default output file if using a filename. If using a handle, will set the default output file with the given handle. If called without any arguments, it returns the default output file handle.
- read(value)
- Reads a line or set of lines in the file. The lines read can be decided based on what is entered as the first parameter, these options include:
- “*all”: reads the whole file
- “*line”: reads the next line
- “*number”: reads a line at the index number
- Number Value: reads a string with up to that number of characters
- Reads a line or set of lines in the file. The lines read can be decided based on what is entered as the first parameter, these options include:
- tmpfile()
- Returns a handle to a temporary file that will be deleted once the script is complete.
- type(obj)
- Checks whether the given object is a valid file handle. Returns the string “file” if the object is an open file handle, the string “closed” if a closed file handle, or nil if not a file handle.
- write(value, value2, value3, …)
- Writes to the file the given values.
Pixera 2.0.172 | 18. November 2024 | C.L.