Difference between revisions of "X File"

From GiderosMobile
(added x_ before file because file: is a wiki reserved word)
(expanded example)
Line 4: Line 4:
 
'''<translate>Supported platforms</translate>:''' [[File:Platform android.png]][[File:Platform ios.png]][[File:Platform mac.png]][[File:Platform pc.png]][[File:Platform html5.png]][[File:Platform winrt.png]][[File:Platform win32.png]]<br/>
 
'''<translate>Supported platforms</translate>:''' [[File:Platform android.png]][[File:Platform ios.png]][[File:Platform mac.png]][[File:Platform pc.png]][[File:Platform html5.png]][[File:Platform winrt.png]][[File:Platform win32.png]]<br/>
 
'''<translate>Available since</translate>:''' Gideros 2011.6<br/>
 
'''<translate>Available since</translate>:''' Gideros 2011.6<br/>
 +
 
=== <translate>Description</translate> ===
 
=== <translate>Description</translate> ===
<translate>file object is usually returned by [[Special:MyLanguage/io.open|io.open]] used to manipulate (read and write) files in lua</translate>
+
<translate>file object is usually returned by [[Special:MyLanguage/io.open|io.open]] used to manipulate (read and write) files in lua.</translate>
 +
 
 
=== <translate>Examples</translate> ===
 
=== <translate>Examples</translate> ===
'''Copy a file'''<br/>
+
<source lang="lua">
<source lang="lua">local function copy(src, dst)
+
--function to copy file
local srcf = io.open(src, "rb")
+
local function copy(src, dst)
local dstf = io.open(dst, "wb")
+
    local srcf = io.open(src, "rb")
local size = 2^13     -- good buffer size (8K)
+
    local dstf = io.open(dst, "wb")
while true do
+
 
local block = srcf:read(size)
+
    local size = 2^13   -- good buffer size (8K)
if not block then break end
+
    while true do
dstf:write(block)
+
        local block = srcf:read(size)
end
+
        if not block then break end
srcf:close()
+
        dstf:write(block)
dstf:close()
+
    end
end</source>
+
 
 +
    srcf:close()
 +
    dstf:close()
 +
end
 +
 
 +
--function to check if file exists
 +
local function exists(file)
 +
    local f = io.open(file, "rb")
 +
    if f == nil then return false end
 +
    f:close() return true
 +
end
 +
 
 +
--usage
 +
if not exists("|D|database.db") then
 +
    copy("database.db", "|D|database.db")
 +
end
 +
</source>
 +
 
 
{|-
 
{|-
 
| style="width: 50%; vertical-align:top;"|
 
| style="width: 50%; vertical-align:top;"|

Revision as of 03:56, 1 July 2020


Supported platforms: Platform android.pngPlatform ios.pngPlatform mac.pngPlatform pc.pngPlatform html5.pngPlatform winrt.pngPlatform win32.png
Available since: Gideros 2011.6

Description

file object is usually returned by io.open used to manipulate (read and write) files in lua.

Examples

--function to copy 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

--function to check if file exists
local function exists(file)
    local f = io.open(file, "rb")
    if f == nil then return false end
    f:close() return true
end

--usage
if not exists("|D|database.db") then
    copy("database.db", "|D|database.db")
end

Methods

file:close closes file
file:flush saves any written data to file
file:lines returns iterator function to return lines, nil ends
file:read reads file according to given formats, returns read values or nil
file:seek sets file pos, whence="set"|"cur"|"end", defaults "curr",0, returns file pos
file:write writes strings or numbers to file

Events

Constants