Difference between revisions of "Io.open"
From GiderosMobile
| (10 intermediate revisions by 2 users not shown) | |||
| Line 1: | Line 1: | ||
| __NOTOC__ | __NOTOC__ | ||
| '''Available since:''' Gideros 2011.6<br/> | '''Available since:''' Gideros 2011.6<br/> | ||
| + | '''Class:''' [[io]]<br/> | ||
| + | |||
| === Description === | === Description === | ||
| − | This function opens a file | + | This function opens a file in the specified mode. The function returns a new file handle, or nil plus an error message. | 
| + | <syntaxhighlight lang="lua"> | ||
| + | (file) = io.open(filename, mode) | ||
| + | </syntaxhighlight> | ||
| + | The '''mode''' string can be any of the following: | ||
| + | *"r": read mode (the 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. | |
| − | + | This string is exactly what is used in the standard C function fopen. | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| === Parameters === | === Parameters === | ||
| − | '''filename''': (string) filename to open <br/> | + | '''filename''': (string) filename to open<br/> | 
| − | '''mode''': (string) mode in which to open the file '''optional'''<br/> | + | '''mode''': (string) mode in which to open the file '''optional''' (default = "r")<br/> | 
| + | |||
| === Return values === | === Return values === | ||
| '''Returns''' (file) file object<br/> | '''Returns''' (file) file object<br/> | ||
| + | |||
| + | === Example === | ||
| + | '''To copy a file''' | ||
| + | <syntaxhighlight lang="lua"> | ||
| + | local function copy(src, dst) | ||
| + | 	local srcf = io.open(src, "rb") | ||
| + | 	local dstf = io.open(dst, "wb") | ||
| + | 	local size = 2^13 -- good buffer size (8K) | ||
| + | 	while true do | ||
| + | 		local block = srcf:read(size) | ||
| + | 		if not block then break end | ||
| + | 		dstf:write(block) | ||
| + | 	end | ||
| + | 	srcf:close() | ||
| + | 	dstf:close() | ||
| + | end | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | {{Io}} | ||
Latest revision as of 05:28, 30 July 2025
Available since: Gideros 2011.6
Class: io
Description
This function opens a file in the specified mode. The function returns a new file handle, or nil plus an error message.
(file) = io.open(filename, mode)
The mode string can be any of the following:
- "r": read mode (the 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.
This string is exactly what is used in the standard C function fopen.
Parameters
filename: (string) filename to open
mode: (string) mode in which to open the file optional (default = "r")
Return values
Returns (file) file object
Example
To copy a file
local function copy(src, dst)
	local srcf = io.open(src, "rb")
	local dstf = io.open(dst, "wb")
	local size = 2^13 -- good buffer size (8K)
	while true do
		local block = srcf:read(size)
		if not block then break end
		dstf:write(block)
	end
	srcf:close()
	dstf:close()
end
